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:
/**
@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
/**
@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(){
returnthis->key;
}
/**
Gets the value of this Entry
@return - The value of this Entry
*/
template <class a, class b> b Entry<a, b>::getValue(){
returnthis->value;
}
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?
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:
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.
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.