2

This does not work. It gives an error regarding ISO C++ forbidding initialization.

class hash_map 
{
private:
    hash_entry **table;
    const int TABLE_SIZE = 128;
public:
    hash_map() 
    {
        table = new hash_entry*[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++)
            table[i] = NULL;
    }
    int get(int key) 
    {
        int hash = (key % TABLE_SIZE);
        while (table[hash] != NULL && table[hash]->getKey() != key)
            hash = (hash + 1) % TABLE_SIZE;
        if (table[hash] == NULL)
            return -1;
        else
            return table[hash]->getValue();
    }
    void put(int key, int value) 
    {
        int hash = (key % TABLE_SIZE);
        while (table[hash] != NULL && table[hash]->getKey() != key)
            hash = (hash + 1) % TABLE_SIZE;
        if (table[hash] != NULL)
            delete table[hash];
        table[hash] = new hash_entry(key, value);
    }
    ~hash_map() 
    {
        for (int i = 0; i < TABLE_SIZE; i++)
            if (table[i] != NULL) delete table[i];
        delete[] table;
    }
};
ildjarn
  • 62,044
  • 9
  • 127
  • 211

5 Answers5

8
const int TABLE_SIZE = 128;

This is the cause of the compilation error. It is allowed in C++11 only, not in C++03 and C++98.

Either make it a static member of the class, OR initialize it in the constructor. Make use of member-initialization-list for it.

Apart from that dont forget to implement copy-semantics following Rule of Three, OR disable it altogether by declaring them (don't define them) in the private section. I think, disabling it would make more sense in this case.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • works...thanks...one would think const implies static to the compiler –  Oct 17 '11 at 17:15
  • 1
    @ChrisAaker: No, each instance of a class can have a different `const int TABLE_SIZE` if it's initialized in the constructor. (Though I agree if it's initialized in place like you do) – Mooing Duck Oct 17 '11 at 17:17
  • 1
    @Chris: For global variables, it does (although `static` has a very different meaning there). Member variables are only static if you declare them so - you sometimes want non-static constant members. – Mike Seymour Oct 17 '11 at 17:18
  • 1
    @Chris Not necessarily, what of the case where the value can be different across class instances? – MGZero Oct 17 '11 at 17:18
  • ummm I thought const was used for things like pi=3.14...constants...same across all instances –  Oct 17 '11 at 17:23
  • Well yea, that's a case where you would want it to be the same across all, and should be static. But how about for example, in a game environment. If say, an NPC should be in some constant state, but you'd want each instance to be in a different constant state. I.e, NPC1 is in state 1, NPC2 is in state 2 etc. I.e your constructor would use a variable in the initializer list, not a hardcoded value. – MGZero Oct 17 '11 at 18:04
4

You need to initialize it in constructor, change

const int TABLE_SIZE = 128;

to

const int TABLE_SIZE;

and the constructor from

hash_map() 
  {
  table = new hash_entry*[TABLE_SIZE];
  for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL;
  }

to

hash_map() : TABLE_SIZE(128)
  {
  table = new hash_entry*[TABLE_SIZE];
  for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL;
  }
Daniel
  • 30,896
  • 18
  • 85
  • 139
0

You need to put the initialization into the constructor like this:

class hash_map 
  {
  private:
    hash_entry **table;
    const int TABLE_SIZE;
  public:
    hash_map(): TABLE_SIZE(128) 
      {
      table = new hash_entry*[TABLE_SIZE];
      for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL;
      }
  ...
StevieG
  • 8,639
  • 23
  • 31
0

You could have been more specific and provide a minimal example that reproduces the problem and an exact error that compiler gives you. But anyway, even without a psychic mode turned on, it is obvious that error in on this line:

const int TABLE_SIZE = 128;

You cannot initialize a class member like that, it has to be done in constructor initialization list, unless this member is a static constant compile-time expression (constexpr in C++11).

So remove = 128 from that line and modify constructor to do this:

hash_map() : TABLE_SIZE (128)
      {
....
0

Use enumerated type:

class hash_map
{
  private:
    enum
    {
        TABLE_SIZE = 128;
    };
  hash_entry * table[TABLE_SIZE];
//...
};
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154