Unresovled external symbol

I am trying to learn C++, but i can't get this to work. From my understanding, it is conventional to put prototypes in a header file, and then have the implementations in a C++ source file. So here is my problem:

I have "Entry.h", which provides the prototypes:
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

/**
	@Author:								Craig Naumann
	@Date:									02/04/2012
	@Description:			
		Provides an entry template for Maps and Lists.
*/

#ifndef _ENTRY_H_
#define _ENTRY_H_

template <class a, class b> class Entry{
public:
	Entry( a key, b value );					//Constructor

	a getKey();									//Gets the key of this entry
	b getValue();								//Gets the value of this entry

	void setKey( a key );						//Sets the key of this entry
	void setValue( b value );					//Sets the value of this entry

protected:

private:
	a key;										//The key of this entry
	b value;									//The value of this entry

};
#endif 


And then the implementations in Entry.cpp:
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
47
48
49
50
51
52
53
54
55
56
/**
	@Author:								Craig Naumann
	@Date:									02/04/2012
	@Description:			
		Provides an fuctionality for Entry template in "Entry.h"
*/

#include "Entry.h"

/**
Constructs this Entry
@param key - The key to construct this entry with
@param value - The value to construct this entry with
*/
template <class a, class b> Entry<a, b>::Entry( a key, b value ){
	this->key = key;
	this->value = value;

}

/**
Sets the key of this Entry
@param key - The key to set as this Entry's key
*/
template <class a, class b> void Entry<a, b>::setKey( a key ){
	this->key = key;

}


/**
Sets the value of this Entry
@param value - The value to set as this Entry's value
*/
template <class a, class b> void Entry<a, b>::setValue( b value ){
	this->value = value;

}

/**
Gets the key of this Entry
@return - The Key of this Entry
*/
template <class a, class b> a Entry<a, b>::getKey(){
	return this->key;

}

/**
Gets the value of this Entry
@return - The value of this Entry
*/
template <class a, class b> b Entry<a, b>::getValue(){
	return this->value;

}


And then i have main.cpp just to test it out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Entry.h"

using namespace std;

void main(){

	Entry<int, int> entry( 12, 22 );

	cout << "Entry key is: " << entry.getKey() << endl << "Entry value is " << entry.getValue() << endl;

	cin.get();

}



And when i try to run main.cpp, i get error messages:
1
2
3
4
5
1
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall Entry<int,int>::getKey(void)" (?getKey@?$Entry@HH@@QAEHXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall Entry<int,int>::getValue(void)" (?getValue@?$Entry@HH@@QAEHXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Entry<int,int>::Entry<int,int>(int,int)" (??0?$Entry@HH@@QAE@HH@Z) referenced in function _main
1>c:\users\craig\documents\visual studio 2010\Projects\GLI\Debug\GLI.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


But the thing is, if i put the implementations in the Header file, it works just fine. Is there something wrong with the order that the compiler loads the files? What can i do to fix this?
Last edited on
You're not compiling and/or linking to Entry.cpp

Since you're using Visual Studio, I expect this means that it's not in the project or some other such IDE-speak.
Last edited on
Thanks for the reply Moschops, How do i link Entry.cpp? I cant find anything about how to do it in the project properties. Everything is in the same project, here is an image of the project explorer:

http://gyazo.com/f202e4fd78e422a1555d16061b6243f6
Not a clue, I'm afraid. I don't use Visual Studio. If it's any consolation, once compiled and linked, your code generates a program with no complaints.
Alright, thanks moschops.
I've never used templates, but on line 8 of main;
Entry<int, int> entry( 12, 22 );

Don't you call it like:
entry( 12, 22 );?

I also use VS, and if you create the .cpp/.h/whatever extension within VS, it's included in the project and compiled as a whole. Even if you don't link to the newly created file in any way, and this file has errors, your compile will fail.
Since your class is a template class, you can't separate your definition from your declaration. You must both declare & define the entire template class in the header.

See here for a better explanation than I could fit in here:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
Last edited on
Topic archived. No new replies allowed.