Mar 30, 2012 at 4:12am UTC
Hi guys. I'm having problem with template. Please enlighten me. The files are listed below.
BinaryTree.h
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
#include <algorithm>
using namespace std;
template<class T> struct Node
{
T info;
Node<T> *lLink;
Node<T> *rLink;
};
template<class T> class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
.
.
.
BinaryTree.cpp
#include <iostream>
#include "BinaryTree.h"
using namespace std;
template<class T> BinaryTree<T>::BinaryTree() { root = NULL; }
.
.
.
BinarySearchTree.h
#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H
#include <iostream>
#include "Map.h"
#include "BinaryTree.h"
using namespace std;
template<class T> class BinarySearchTree: public BinaryTree<T>
{
public:
BinarySearchTree();
~BinarySearchTree();
private:
};
#endif
BinarySearchTree.cpp
#include <iostream>
#include "BinarySearchTree.h"
using namespace std;
template<class T> BinarySearchTree<T>::BinarySearchTree(): BinaryTree() { }
template<class T> BinarySearchTree<T>::~BinarySearchTree() { }
Main.cpp
#include <iostream>
#include "BinarySearchTree.h"
using namespace std;
int main()
{
BinarySearchTree<Map> test;
system("pause");
return 0;
}
I will get an error:
Main.obj : error LNK2019: unresolved external symbol "public: __thiscall BinaryTree<class Map>::BinaryTree<class Map>(void)" (??0?$BinaryTree@VMap@@@@QAE@XZ) referenced in function "public: __thiscall BinarySearchTree<class Map>::BinarySearchTree<class Map>(void)" (??0?$BinarySearchTree@VMap@@@@QAE@XZ)
Main.obj : error LNK2019: unresolved external symbol "public: __thiscall BinaryTree<class Map>::~BinaryTree<class Map>(void)" (??1?$BinaryTree@VMap@@@@QAE@XZ) referenced in function "public: __thiscall BinarySearchTree<class Map>::~BinarySearchTree<class Map>(void)" (??1?$BinarySearchTree@VMap@@@@QAE@XZ)
C:\Users\Faizal Rahman\documents\visual studio 2010\Projects\Assg2v2\Debug\Assg2v2.exe : fatal error LNK1120: 2 unresolved externals
What is happening? :(
Mar 30, 2012 at 5:25am UTC
One of the properties of templates is that it inlines all of the functions. That means that it needs to be declared AND defined in the header. If it is defined in a source file, then the linker won't understand what is going on.
You'll need to stick your template functions in your header.
One thing that I noticed the SFML library source does which I like is that they have a seperate file for inline functions. So they have .h header files, .cpp source files, and .inl inline files. All inline functions are in the .inl file which is included at the bottom of the .h file like so:
BinaryTree.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
#include <algorithm>
using namespace std;
template <class T> struct Node
{
T info;
Node<T> *lLink;
Node<T> *rLink;
};
template <class T> class BinaryTree
{
public :
BinaryTree();
~BinaryTree();
.
.
.
#include "BinarySearchTree.inl"
BinarySearchTree.inl
1 2 3 4 5
#include <iostream>
template <class T> BinarySearchTree<T>::BinarySearchTree(): BinaryTree() { }
template <class T> BinarySearchTree<T>::~BinarySearchTree() { }
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include "BinarySearchTree.h"
using namespace std;
int main()
{
BinarySearchTree<Map> test;
system("pause" );
return 0;
}
Any non-template functions would go in BinarySearchTree.cpp
Last edited on Mar 30, 2012 at 5:26am UTC
Mar 30, 2012 at 10:00am UTC
Thank you for the prompt reply! I'm working in a Visual Studio 2010 environment. I can't seem to find an .inl extension. Is there any other ways around this.
Mar 30, 2012 at 10:30am UTC
You have to put the function definitions in the header files.