CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    template member functions in a non template class

    I am working on an assignment and this problem has me puzzled. I am still working on working on template functions. Mostly within classes. I am seeking help on where my error is. Here is my code.

    In my class header I have:

    class predator
    {
    public:

    template<typename t>
    void hunt(t & prey);
    // other stuff...

    private:
    // more stuff
    }

    In my definition I have this:

    template <typename t>
    void predator::hunt(t & prey)
    {
    // what I want it to do here
    }

    And to use the function I have something like:

    predatorName.hunt(preyName);


    Now when I compile (I have been using Dev c++) I get the following error:

    Linker error: Undefined reference to 'void predator::hunt<predator>(predator&)'

    Any hints on what I am doing wrong. Thanks very much in advance.

    Kat

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: template member functions in a non template class

    Should work, so it could be a lot of things. The best you could do is break it down to the smallest example, and see if it works. If it doesn't, you could always post your COMPLETE code.

    Code:
    class predator
    {
    public:
        template<typename t>
        void hunt(t& prey);
    };
    
    template <typename t>
    void predator::hunt(t& prey)
    {
    }
    
    
    int main()
    {
        predator pred;
        int a = 5;
        pred.hunt(a);
    }
    This works no problem for me.

  3. #3
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: template member functions in a non template class

    With templates, you cant split the code into .h and .cpp files like you can with non-templates without export which virtually no compiler supports, or ever will, though comeau does.

    When you include the .h in a file and try to use the templated function, the compiler tries to instantiate the template substituting the generic type(s) for specific type(s) but it cannot do that unless it can see the definition of the template.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  4. #4
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: template member functions in a non template class

    Post implementation of void predator::hunt(t & prey).

    Or alternative way is to do it in OO way, have a base class Prey and inherit from it different kind of preys and have void predator::hunt(Prey& prey)

  5. #5
    Join Date
    Apr 2010
    Posts
    3

    Re: template member functions in a non template class

    Well, some good news and some bad.

    If I just declare the function and its definition it compiles. However as soon as I use it, I get the linker error.

    For example the part where you have:

    int main()
    {
    predator pred;
    int a = 5;
    pred.hunt(a);
    }

    I get the linker error again. But, just creating the function and not using it yet, does not cause an error.

  6. #6
    Join Date
    Apr 2010
    Posts
    3

    Re: template member functions in a non template class

    Quote Originally Posted by Russco View Post
    With templates, you cant split the code into .h and .cpp files like you can with non-templates without export which virtually no compiler supports, or ever will, though comeau does.

    When you include the .h in a file and try to use the templated function, the compiler tries to instantiate the template substituting the generic type(s) for specific type(s) but it cannot do that unless it can see the definition of the template.
    This must be the issue. Thanks for this information. Frustrating since the instructor wants us to do it this way. Class header and class .cpp file. And I am using the templated function in another class .cpp file, for example I have a world class that uses the predator class. All different files of course.
    ie. predator.h, predator.cpp, world.h, world.cpp

  7. #7
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: template member functions in a non template class

    In the .h put the class dec and the template definition. in the class.cpp file put the non-template definitions. In the main.cpp file include the .h and use your class. it should work fine.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured