A using directive is basically a statement of the form:What is a directive?
using namespace namespace_name;
It makes all names in namespace_name (e.g., std) accessible.
A using directive is basically a statement of the form:What is a directive?
using namespace namespace_name;
It makes all names in namespace_name (e.g., std) accessible.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am working on a template to accomplish the same thing as the overloaded function - namely accept and display two parameters. This time I will write this as a non-member function.
I have this in my header file (I haven't removed the directive yet, until I know how to fully qualify the template)
for definitionCode:template<class DTYPE1 text, class DTYPE2 someVal>
to call:Code:DTYPE1, DTYPE2 showStuff(DTYPE1 text, DTYPE2 someVal) { cout << text << " " << someVal << endl; }
The C++ book I am working from has next to nothing to say about more than one data type. I am sure that there needs to be a template argument list but how?Code:showStuff(string "Text ", int 7); showStuff(string "More text ", double 36.65);
Last edited by marQade; 12-03-2007 at 01:08 PM. Reason: oops.
That is incorrect.
A function can only return one type.
A template functions look like this:
Code:template<typename NameOfTypeHere1, typename NameOfTypeHere2> ReturnTypeHere foo(NameOfTypeHere1 myfirstarg, NameOfTypeHere2 myarg2);
Ha, so there are STILL no shortcuts in the header file? A template only shortens the definitions?
Huh? What's that supposed to mean?
A template is used to allow the compiler to substitute the type and generate code instead of you making lots of overloaded functions.
What I was thinking there for a minute was that my header file would contain the template prefix, followed by, well basically by more declarations that look like overloaded functions, then the definition would only contain a template definition. I'll try to work this out again and come back to you.
Every function, declaration or definition must have the exact same structure, and thus the "template" part must also appear before every declaration as well as definition.
Here's a sample:
This is a sample from my own code - a template class. Adding the template to the class basically adds the "template" part before all the functions.Code:// Declaration: template<typename T> class CMemoryManagerBase abstract { // ... void SetNewPointer(T* pNew) throw(...); // ... } // Definition: #define MMB(return_type) template<typename T> return_type CMemoryManagerBase<T> MMB(void)::SetNewPointer(T* pNew) throw(...) { // ... }
See the definition. I also put the template part before there and the function looks exactly like the declaration (although I use a MACRO to shorten the name).
That's how you use templates.
You can have more than one template, as I showed. The principle is the same.
Last edited by Elysia; 12-03-2007 at 01:27 PM.
Thank you for your patience with me...
so far I have this much done:
in my header file, andCode:template<class T1, class T2> void showStuff(T1 text, T2 someVal)
Then call these like so:Code:template<class T1, class T2> void showStuff(T1 text, T2 someVal { cout << text << " " << someVal << endl; }
and receive an external error message.Code:showStuff("Text here ", 36.52); showStuff("More text ", 7);
Don't forget ; on your declarations.
Templates should also reside in headers. There is an excellent tutorial on why, but unfortunately, I don't have a link to it.
Okay, I'm heading out to class again...but I will come back to you in about 2 hours. I placed the ';' after the 'void showStuff(T1 text, T2 someVal);' but still get an external error. Do you mean that my header file should contain something like
I will look for a tutorial this afternoon.Code:template<class T1, class T2> void showAbs(T1 text, T2 someVal) { cout << text << " " << someVal << endl; }
Place both declaration and definition in the header, or just skip the declaration and only do your definition (works fine for templates).
I understand now! Thank you so much for all your time and patience.