Hi, i recieve and undefined refrence error when i try to link files, and i think it is due to the use of templates. I have a .h file which has 2 classes and there respective functions in them, however i want to place the functions into a .cpp file. A third cpp file then needs to use these functions, however thats when i get the errors.

Here is the h file, BinaryTree.h

#ifndef BINARYTREEITERATOR_H


template <class T>
class BinaryTreeIterator
{
	public:
		BinaryTreeIterator(BinaryTree<T>&);	// constructor
		void SetIterator();			// find left most node
		T Next();				// return next data item
		bool More();				// are there any more nodes?
	private:
		typename BinaryTree<T>::nodePtr curr;
		BinaryTree<T> *t;
};

template <class T>
BinaryTreeIterator<T>::BinaryTreeIterator(BinaryTree<T>& bintree)
{
	t = &bintree;
	curr = 0;
}

template <class T>
void BinaryTreeIterator<T>::SetIterator()
{
	curr = t->root;
	if (curr != 0)
		while (curr->left != 0)
			curr = curr->left;
}

template <class T>
T BinaryTreeIterator<T>::Next()
{
	T rvalue = curr->data;

	if (curr->right != 0)			// into the right subtree
	{
		curr = curr->right;
		while (curr->left != 0)
			curr = curr->left;
	}
	else if (curr->parent == 0)		// no parent
		curr = 0;			// we are finished
	else					// find ancestor
	{
		while (curr->parent != 0
				&& (curr->parent)->left != curr)
			curr = curr->parent;
		curr = curr->parent;
	}
	return rvalue;
}		

template <class T>
bool BinaryTreeIterator<T>::More()
{
	return (curr != 0);
}	

#define BINARYTREEITERATOR_H
#endif

SymbolTable.cpp which uses the functions

#include <cstring>
#include <iostream>
#include <fstream>
#include "BinaryTree.h"
#include "SymbolTable.h"
#include "Program.h"
#include "Words.h"
using namespace std;

SymbolTable::SymbolTable(const char filename[])
{
	File(filename);
}

SymbolTable::~SymbolTable()
{
}

void SymbolTable::File(const char filename[])
{
	Prog.Open(filename);
}

void SymbolTable::Make()
{

	int line;
	char token[200];
	char entry[200];
	fstream fopen;
	Word* tempword;
	Identifier* templine;
	ReservedWord* rev;
	
	
	fopen.open("ReservedWords.txt");
	if (!fopen.good())
	{
		cout <<"file not found";
		exit(1);
	}
	fopen >> entry;
	while(!fopen.eof())
	{
		rev = new ReservedWord(entry);
		fopen >> entry;
	}
	fopen.close();
	
	while(Prog.GetNextToken(token,line))
	{
		templine = new Identifier(token,line); 
		
		if (ST.Locate((WordPtr)templine,tempword))	
		{	
			delete templine;
			tempword->Update(line);
		}
		else
		{	
			templine->Update(line); 
			ST.Insert((WordPtr)templine);
		}
	}
}

void SymbolTable::Print()
{

	BinaryTreeIterator<WordPtr> It(ST);
	It.SetIterator();
	
	while(It.More())
	{
		WordPtr w = It.Next();						
		w->Print(cout);
		delete w;
	}
}

I want to split the BinaryTree.h so the functions are in a seperate cpp file, but when i do this, the SymbolTable.cpp does not recognise the functions, despite having the BinaryTree.h included in the headers.

Tried the solution in the above link,

export template<class T>

The compiler im using didnt recognise it :(
Any one know of a solution(apart from putting the functions in the same file as the .h)??

I was thinking of possibly removing the template and using void* in my functions, would this be feesable?

You can use a #include to include your SymbolTable.cpp into whichever files it is called from. The problem is the compiler does not create the code from the template until a function calls for it and if you compile it in a .o or anything before it is called, the functions won't exist. See Professional C++ by Solter and Kleper for an excellent explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.