can not make a default constructor

here is the hash table implementation
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
57
58
59
60
61
62
63

#ifndef HashTable_h
#define HashTable_h

#include <list>
#include <algorithm>
#include <vector>
using namespace std;

template <class KEY, class VALUE, int CAPACITY>
class HashTable
{
private:
	struct Node
	{
		KEY key;
		VALUE value;
		Node *next;
		//to support STL find, add this to Node
		void operator=(const VALUE& v) {value = v};
		bool operator==(const Node& n)const {return key == n.key}

		Node(): key(KEY()), value(VALUE()), next(NULL){}
	};
	int size;
	list<Node> data[CAPACITY];
	int (*hashCode)(const KEY &);	// as data member, "hashCode"
	int getWrappedIndex(int) const;
	

public:
	HashTable(int (*)(const KEY &));	// constructor prototype
	VALUE & operator[](const KEY &);
	VALUE operator[](const KEY &)const;
	int Size() const;
	bool containsKey(const KEY &) const;
	void deleteKey(const KEY &);
	void clear();
	vector<KEY> keys() const;

	
};

//getWrappedIndex function
template <class KEY, class VALUE, int CAPACITY>
int HashTable<KEY, VALUE, CAPACITY>::getWrappedIndex(int index)const
{
	int wi = hashCode(KEY) % CAPACITY;
	if (wi < 0)
		wi += CAPACITY;
	return wi;
}

// constructor
template <class KEY, class VALUE, int CAPACITY>
HashTable<KEY, VALUE, CAPACITY>::HashTable(int (*hfunc)(const KEY & key))
{
	size = 0;
	hashCode = hfunc;
}

#endif


i followed my teacher's instruction to do this implementation, and i checked it many time i think it has no error

and this is my main.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
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#include "HashTable.h"
#include "HashTable.h"	//ifndef test

void printLine()
{
	cout << endl;
	for (int i = 0; i < 50; i++)
		cout << "-";
	cout << endl;
}

struct TermSection
{
	string course;
	string term;
	string section;
	string instructor;
	bool operator==(const TermSection &a) const
	{
		return course == a.course && term == a.term && section == a.section;
	}
};


int hash(const TermSection &key)
{
	int sum = 0;
	char t[20];
	strcpy(t, key.term.c_str());
	char s[20];
	strcpy(s, key.section.c_str());
	char buf[40];
	strcpy(buf, strcat(t, s));
	for(int i = 0; i < strlen(buf); i++)
		sum = sum + (int)buf[i];
	return sum;
}


int main()
{
	cout << "Hello World!" << endl;

	//Code Blocks
	printLine();

	HashTable<TermSection, int, 1000> hashTable(hash);

}


im tring to make a default construction first and this is my first thing to do in my test driver. btw im using visual studio 2012. After i tried to build the program and the compiler output these messages (error C2872: 'hash' : ambiguous symbol) AND (int hash(const TermSection &)' or 'd:\microsoft visual studio 11.0\vc\include\xstddef(234) : std::hash')

Is this my compiler problem? or my syntax error?
Can anyone tell me which part i did wrong? and what is ambiguous symbol for my hash??
This is one of the problems with using namespace std;. The compiler doesn't know if you mean your own hash function or std::hash. When passing the function as argument to the constructor you could specify that you mean the function in the global namespace by putting :: in front of the function name .
Last edited on
thanks peter87, i understand now
Topic archived. No new replies allowed.