Results 1 to 6 of 6

Thread: Template problem

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2001
    Posts
    46

    Template problem

    class CAnyBaseClass
    {
    public:
    CAnyBaseClass();
    ~CAnyBaseClass();
    };

    // Definitions ...

    template<class BASE>
    class CMyClass : public BASE
    {
    public:
    CMyClass();
    ~CMyClass();
    };

    // Definitions etc...


    ...

    CMyClass<CAnyBaseClass>* pMyClass = new CMyClass<CAnyBaseClass>;

    causes LNK2019 linker error.

    I don`t know what to do!
    Please, help !

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    prolly your defintions there which you haven't posted
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2001
    Posts
    46
    With definitions I mean definitions of constructor and destructor

    CAnyBaseClass::CAnyBaseClass()
    {
    }

    CAnyBaseClass::~CAnyBaseClass()
    {
    }

    and

    CMyClass::CMyClass()
    {
    }

    CMyClass::~CMyClass()
    {
    }

    I have just written them only to avoid compiler error, so I haven`t written any specific code inside them.

    I tried to put the defintions in the header file of the class, and that avoided the error. But when they were in the .cpp file, linker gives error.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    don't forget that CAnyBaseClass is a template class, the function definitions are thus templates as well
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    ALL template code goes in headers, always. Since the template code is only compiled when it is used in your program, if you put it in a source file, it wont be found when the compiler tries to compile it. It will compile, since the prototype exists, but the implementation is not there.

    Z.

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2001
    Posts
    46
    Thanks a lot!

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