Unresolved external symbol

The following code does not compile in VS 2010 Visual C++.

I get the error:

error LNK2019: unresolved external symbol "public: __thiscall testTemplate<double>::testTemplate<double>(void)" (??0?$testTemplate@N@@QAE@XZ) referenced in function _main main.obj

But if I move the code between

// Begin Comment

and

// End Comment

into the file main.cpp, the code compiles.

I am not sure why the compiler is complaining about the testTemplate class but not about the test class.

I appreciate any help in this matter. Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//File test.h

#include "stdafx.h"

template<typename T>
class testTemplate
{ protected: T templateKey_;
public : testTemplate();
};

class test
{
protected: int key_;
public: test();
};
//----------------------------------

//File test.cpp

#include <iostream>
#include "test.h"

test::test() {	std::cout << "test object created." << std::endl;
				key_ = -1; };
// Begin Comment

template<typename T>
testTemplate<T>::testTemplate() : templateKey_(-5) {	std::cout << "testTemplate object created." << std::endl; };

// End Comment

//------------------------------------
// File main.cpp

#include "stdafx.h"
#include <iostream>
#include "test.h"

int _tmain(int argc, _TCHAR* argv[])
{
	test tc = test();
	testTemplate<double> tct = testTemplate<double>();	
	std::cin.get();
	return 0;
}
//------------------------------------------------------ 

closed account (zb0S216C)
You should never place template definitions within a source module as it causes URES errors. Make sure that template definitions are within the same file as the class/function declaration. You can also in-line template definitions by placing them (definitions) within a file (.inl) and #include it beneath the class/function.

Actually responding to your error would help wouldn't it? :)P

By looking at your error, it seems as though the compiler invoked the default constructor of testTemplate. However, the linker cannot find the definition of testTemplate< T >::testTemplate( ). In order to fix this, you must do what I said before (above).

Wazzak
Last edited on
Compiler doesn't know type of T. But you are trying to initialize templateKey. T might be a pointer, char array, a class... You can't assign -5.
Topic archived. No new replies allowed.