Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

linking problem with a template

Posted on 2006-11-08
2
Medium Priority
?
308 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

715 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