Default Copy constructor: coping a value to array'index]

Hi I am writing a hashtable implementation and the defaul copyconstructor looks like this

template <typename T, size_t MAX_HASH_SIZE>
class HashTable {
HashTable(T data, T (*hash_fn)(T,size_t): array[data](data) {}

private:
int array[MAX_HASH_SIZE]
}


I am getting an error in the highlighted part, it says

error: expected '(' before '[' token

So I cant directly pass an value to array[index]....

technically both array[index] and data are of the same type 'T', in my case int....

Your help is appreciated.

Thanks
You're missing the closing round bracket on the HashTable constructor declaration (before the colon).
No I hardcoded it.... I am sorry, here is my exact implementation

template <typename T, size_t MAX_HASH_SIZE>
class HashTable {

public:
HashTable(T data, T (*hash_fn)(T,size_t)): array[data](data) { }

//[hash_fn(data,MAX_HASH_SIZE)](data) { }

private :
int array[MAX_HASH_SIZE];
size_t count;

};


PS: I missed the brace while closing it....
OK, arrays are indexed by int (or unsigned int), but in the constuctor you're trying to index it by type T, which may not be an int.

Also, you can't initialise like that - you'll have to move the assignment to the body of the constructor. Also, I suspect you want to use the hash_fn to get the index, for example:

1
2
3
4
HashTable(T data, T(*hash_fn)(T, size_t))
{
  array[hash_fn(data, MAX_HASH_SIZE)] = data;
}
And should the array declaration be T array[MAX_HASH_SIZE];, or have I misunderstood the requirements? (Wouldn't be the first time!)
Yeah exactly, I am right now doing what you mentioned above(and my above code is just for finding bugs with the int)...... and I am using the hash_fn too..... I was just wondering why two ints cant copy construct

array[data] = data;

constructor() : array[data](data) { }

if you see both array[data] and data are ints or i.e T is int......

Please change the code

int array[MAX_HASH_SIZE];

to

T array[MAX_HASH_SIZE];

(I was in the mode of debugging and not meant to confuse you, sorry)

Now both the array and data are of type T(int)...... it still shows the same error.... any idea????
You have two problems:

1. You're trying to initialise a member array and C++ doesn't allow direct initialisation of non-static member arrays.

2. You're trying to initialise only one element of the array, which you can't do full-stop - it's all or nothing.

You have to use the array[data] = data; form, which is element assignment and not initialisation.

Cheers,
Jim
Topic archived. No new replies allowed.