Solved

linking problem with a template

Posted on 2006-11-08
2
292 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

786 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