Error : unresolved externals

Hi,

In my program I have the following abstract class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <list>
#include <string>
using namespace std;
#pragma once

template <class T, class K> class AbstractHashTable
{
public:
	virtual int hashFunc(const K&) = 0;
	virtual int search(const K&) = 0;
	virtual bool insert(const T&) = 0;
	virtual bool remove(const K) = 0;
	virtual void printAll() = 0;
};


The following inherited class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
#include "AbstractHashTable.h"
#include "Item.h"

template <class T, class K>
class TableByString : public AbstractHashTable<Item<T, K>, string>
{
protected:
	Item<T, K>* table;
	int size;
	virtual int h1(const string&);
	virtual int h2(const string&);
	int hashString(const string&);
	int hashFunc(const string& key, int);
public:
	TableByString(int);		// CTOR
	int hashFunc(const string& key) { return hashFunc(key, 0); }
	bool isEmpty(int);
	int search(const string&);
	bool insert(const Item<T, K>&);
	bool remove(const string);
	Item<T, K>& operator[](int i) { return table[i]; }	// Returns index in table
	void printAll();
};


And the following main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "TableByString.h"
#include "Professional.h"
#include "Customer.h"

int main()
{
	int size, choice;
	Item<Professional, string> profItem;
	Item<Customer, string> custItem;
	Professional profTmp;
	Customer custTmp;
	string name;
	cout << "Please enter size for professionals hash table: ";
	cin >> size;
	TableByString<Professional, string> ProfHashTable(size); // *this
}


When I delete "*this" row - I have no errors. Otherwise I get these:
Error 2 error LNK1120: 1 unresolved externals D:\...\ConsoleApplication1.exe 1

Error 1 error LNK2019: unresolved external symbol "public: __thiscall TableByString<class Professional,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::TableByString<class Professional,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(int)" (??0?$TableByString@VProfessional@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@H@Z) referenced in function _main D:\...\main.obj


Does anyone know why???

Thanks for helping!
I'm sorry ne555.
I could not understand what do you mean.
I assume you referred me to the "Templates and inline functions" post, but I did not understand from it what am I suppose to do in my case...

Can you be more specific please?
¿where is the definition of the `TableByString' constructor?
Oh... sorry. Here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "TableByString.h"

template<class T,class K>
TableByString<T, K>::TableByString(int num)
{
	bool prm;
	do
	{
		prm = 1;
		for (int i = 2; i < (num / 2); ++i)
		{
			if (!num%i)
			{
				prm = 0;
				break;
			}
		}
		num++;
	} while (!prm);
	size = num;
	table = new Item<T, K>[size];
}


And here's "Item" class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once

enum state { empty, full, deleted };
template<class T, class K>
class Item
{
private:
	T data;
	K key;
	state flag;
public:
	Item(){}
	Item(T d, K k, state f)
	{
		data = d;
		key = k;
		flag = f;
	}
	void edit(T d, K k, state f) 	{ data = d; key = k; flag = f; }
};
Last edited on
You must #include the definition, you cannot have it in a separate cpp
(this is a especial case with templates)


> table = new Item<T, K>[size];
¿where are you deallocating that?
Last edited on
I could be wrong but I thought you'll have to declare / define the actual template classes in header file to make it linkable?
Topic archived. No new replies allowed.