Solved

linking problem with a template

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 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

18 Experts available now in Live!

Get 1:1 Help Now