Thread: Creating Function Templates

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is a directive?
    A using directive is basically a statement of the form:
    using namespace namespace_name;

    It makes all names in namespace_name (e.g., std) accessible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    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)
    Code:
    template<class DTYPE1 text, class DTYPE2 someVal>
    for definition
    Code:
    DTYPE1, DTYPE2 showStuff(DTYPE1 text, DTYPE2 someVal)
    {
        cout << text << " " << someVal << endl;
    }
    to call:
    Code:
    showStuff(string "Text ", int 7);
    showStuff(string "More text ", double 36.65);
    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?
    Last edited by marQade; 12-03-2007 at 01:08 PM. Reason: oops.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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);

  4. #19
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Ha, so there are STILL no shortcuts in the header file? A template only shortens the definitions?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    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.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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:
    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(...)
    {
    	// ...
    }
    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.
    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.

  8. #23
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Thank you for your patience with me...

    so far I have this much done:

    Code:
    template<class T1, class T2>
    void showStuff(T1 text, T2 someVal)
    in my header file, and
    Code:
    template<class T1, class T2>
    void showStuff(T1 text, T2 someVal
    {
       cout << text << " " << someVal << endl;
    }
    Then call these like so:

    Code:
    	showStuff("Text here ", 36.52);
    	showStuff("More text ", 7);
    and receive an external error message.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  10. #25
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    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
    Code:
    template<class T1, class T2>
    void showAbs(T1 text, T2 someVal)
    {
    	cout << text << " " << someVal << endl;
    }
    I will look for a tutorial this afternoon.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Place both declaration and definition in the header, or just skip the declaration and only do your definition (works fine for templates).

  12. #27
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    I understand now! Thank you so much for all your time and patience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM