Templated functions inside a class

This is a discussion on Templated functions inside a class within the C++ Programming forums, part of the General Programming Boards category; I have an item class: Code: class Citem { public: Citem(){}//for briefness, this isn't my exact code template<class Target> bool ...

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Templated functions inside a class

    I have an item class:

    Code:
    class Citem
    {
    public:
    Citem(){}//for briefness, this isn't my exact code
    template<class Target>
    bool Use(Target &target);
    };
    Of course there are other variables and functions, but that is besides the point. When I try to compile, I get an unresolved external error. I know I defined it, and the class I'm passing in has everything required.

    What I did notice is when I hit class view (MSVC++), the Use function wasn't there! Help!
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try inlining the templated class function definition. I know that at least under some conditions MSVC doesn't support definition of templated methods outside of the class declaration.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Very few (if any) C++ compilers will allow you to put template declarations and definitions in different files. It is much more portable just to put the definition where the declaration is (perhaps at the bottom of the file, but still in the same file).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    In my experience the template class can be in a header but most of its function members must also be defined in the header. Sort of like being a newbie to C and putting all functions and code in the header. I, too, have generated several errors in trying to do what you are. I'm not sure why this is - perhaps its because of late binding and the fact that the compiler does not know the type at compile time. Template classes can be a major headache...I think I had to even put the constructor code in the header or it gave me some kind of 'cannot find default constructor' error.

    It's been awhile since I've messed with templates. At one time I thought they were the coolest thing...but they can be a major pain in the rump and generate some weird errors for simple problems.

    You'll sit there and look at the MSVC errors scratching your head and yelling at the compiler.

    By the way is it just me or are the MSVC errors bass ackwards. Borland has much better error messages.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 01:44 PM
  2. Templated member functions
    By Highland Laddie in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2006, 06:25 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Problem with friend functions in templated linked list
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 04:24 PM
  5. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM

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