Hash Table

I'm having trouble running this, I always gets two errors

(1) ...\hash.h(11): error C2061: syntax error : identifier 'string'
(2) ...\main.cpp(14): error C2660: 'Hash::Hashes' : function does not take 1 arguments

can someone point out what is wrong with the code...been trying to figure it out.
hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

#ifndef HASH_H
#define HASH_H

class Hash
{
public:
	int Hashes(string key);


};
#endif 


hash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

#include "hash.h"

int Hash::Hashes(string key)
{
	//int hash = 0;
	//int index; 

	 int index = key.length();

	return index;
}


main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>



#include "hash.h"
using namespace std;
int maint()
{
	int index;
	Hash hashObj;

	index = hashObj.Hashes("ShinAngel");

	cout << "index = " << index << endl;

	return 0;
}
In your header, you need to write std::string.
1
2
3
4
5
6
7
class Hash
{
public:
	// int Hashes(string key);
        int Hashes( std::string key ) ;

};


Ideally, make it const correct int Hashes( std::string key ) const ;
tried what both of you suggested but now I'm getting

(1) >MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

(2)...Debug\Hash Tables.exe : fatal error LNK1120: 1 unresolved externals

also wouldn't writing using namespace std; on my header wouldn't that take care of writing std::, either way I wrote with and without using namespace std; and still got the same errors

Im writing it on visual studio 2010 btw, not sure if
How did you create your project? It looks like you picked some kind of CRT console app setting or something when you were making it (considering the reference to crtexe.obj and ___tmainCRTStartup). Try copying your code into an empty project and then building.
I followed a hash table tutorial on youtube everything is the same other than im not using netbean IDE but vs2010 but Im going to copy the code into a new project and see what happens

edit:
started new a project and received the same errors
Last edited on
Or, alternatively, just configure your project properly.
If you're using VC++, navigate to Project>Properties>Linker>System>SubSystem

EDIT* your problem is on line 8, mate.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>



#include "hash.h"
using namespace std;
int maint()
{
	int index;
	Hash hashObj;

	index = hashObj.Hashes("ShinAngel");

	cout << "index = " << index << endl;

	return 0;
}
Last edited on
Thank you!!! It was drving me nuts. Can't believe I missed that! and line 7 too, using namespace std; should have been before the hash header
and line 7 too, using namespace std; should have been before the hash header

Um... having your header file rely on something else being done before the include statement is a terrible idea. Your header file should be able to be included as is.

You should follow JLBorges' advice - use std::string in your header file.
Topic archived. No new replies allowed.