Solved

linking problem with a template

Posted on 2006-11-08
2
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 clear a vector as well as how to detect empty vectors in C++.

623 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