Templates

This is a discussion on Templates within the C++ Programming forums, part of the General Programming Boards category; Hey guys, Here is what I am doing: Code: template <typename T> void func(vector<T> vec) { .. .. .. } ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    Templates

    Hey guys,
    Here is what I am doing:

    Code:
    template <typename T>
    void func(vector<T> vec)
    {
     ..
     ..
     ..
    }
    Now when I call this function as func<somestruct>(st); I get a compile time error? I am I calling the function wrongly?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,354
    What error? What is "st?" More info please.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    st is the type of somestruct

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    st is the type of somestruct
    If that's true, then it should give errors. Your function would expect a type of vector<somestruct>.

    Post a minimal non-working sample if that doesn't fix it.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Before I had a function in a .h file. Once I move it to a cpp file it works fine. I have done it like this:
    Code:
    template<typename T>
    void func(vector<T>& vec)
    {
    
    }
    
    int main()
    {
       vector<int> vec;
       func(vec);
    }
    Is there a way I can keep the function in an external file

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    Templates must be included in all compilation units that use them. Putting it in a header file and then including the header should be no problem.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Simply moving it to a header file works. But if you put the function declaration in the .h file and definition in .cpp file that does not work.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,790
    But if you put the function declaration in the .h file and definition in .cpp file that does not work.
    That is to be expected. Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp file?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21