Solved

linking problem with a template

Posted on 2006-11-08
2
289 Views
Last Modified: 2010-10-05
Alright a more specific template function. I updated some code following a tutorial but my compiler has a linking error. My linked list class looks like this:

    // h file:
    template <class T>
    class CLinkedList {

        public:
            CLinkedList();
            ~CLinkedList();
    };

    // cpp file:
    template <class T>
    CLinkedList<T>::CLinkedList()
    {
    }

    template <class T>
    CLinkedList<T>::~CLinkedList()
    {
    }

Now in main I just use it like:

    int main()
    {
        CLinkedList<int> list;
        return 0;
    }

But I get two linking errors saying that the constructor/destructor of CLinkedList are undefined. The individual cpp files compile ok, they're just not linking right. I guess I did not define the constructor/destructor correctly? Here's the exact link errors:

1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CLinkedList<int>::~CLinkedList<int>(void)" (??1?$CLinkedList@H@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CLinkedList<int>::CLinkedList<int>(void)" (??0?$CLinkedList@H@@QAE@XZ) referenced in function _main

Thanks
0
Comment
Question by:DJ_AM_Juicebox
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17903273
Even though the C++ standard allows to separate template declarations from template code, hardly any compiler supports that. You will have to either put the definitions from your .cpp file into the header file or add an

#include "mytemplate.cpp"

to the end of "mytemplate.h". Sorry for the bad news.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17903343
gosh
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 138
cb: unreferenced local variable 11 72
C++ get user from AD  (VS6) 7 55
Create a path if not exists 7 69
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now