nonmember function

What does that error mean? I really don't get it.

'hash' : modifiers not allowed on nonmember functions

this hash function is outside of the 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
25
26
27
28
29
30
31
32
33
34
template <typename HashedObj>
class HashTable
{
public:
.
.
.
private
.
.
.

int myhash( const HashedObj & x ) const
{
	int hashVal = ::hash( x );

	hashVal %= array.size( );
	if( hashVal < 0 )
		hashVal += array.size( );

	return hashVal;
}
};   //end of class

template <typename HashedObj>
int hash( const HashedObj & key )  const
{
	int hashVal = 0;

	for( unsigned i = 0; i < key.length( ); i++ )
		hashVal = 37 * hashVal + key[ i ];

	return hashVal;
}
Marking a function as const only makes sense for member functions.
I am really appreciated for your helps today. I fixed that error, too. But now I am getting different errors. Why doesn't just ignore small mistakes and compile it :) Anyway, do you know what can I do to make this hashtable compile with my class HashEntry object?

1
2
3
4
5
6
7
8
9
10
template <typename HashedObj>
int hash( const HashedObj & key )  const
{
	int hashVal = 0;

	for( unsigned i = 0; i < key.length( ); i++ )
		hashVal = 37 * hashVal + key[ i ];

	return hashVal;
}





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
class HashEntry
{
public:
	HashEntry()
	{
		code = 0;
		key = " ";
	}
	
	HashEntry(int c, string k)
	{
		code = c;
		key = k;
	}

	bool operator==(const HashEntry &rhs) const
	{
		return (key == rhs.key);
	}

	bool operator!=(const HashEntry &rhs) const
	{
		return !(key == rhs.key);
	}

	int code;
	string key;
};


The error is

'length' : is not a member of 'HashEntry'

key is a string, why doen't it accept it?
Are you passing HashEntry to hash instead of HashEntry::key?
Thanks buddy, it works now. Finally I can sleep :)
Topic archived. No new replies allowed.