C++ 向类添加常量的正确方法

C++ 向类添加常量的正确方法,c++,C++,这是行不通的。它给出了一个关于ISO C++禁止初始化的错误。 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++)

这是行不通的。它给出了一个关于ISO C++禁止初始化的错误。
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;
    }
};
类哈希映射
{
私人:
散列项**表;
const int TABLE_SIZE=128;
公众:
hash_映射()
{
table=新的散列项*[表大小];
对于(int i=0;igetKey()!=key)
哈希=(哈希+1)%TABLE\u SIZE;
if(表[hash]==NULL)
返回-1;
其他的
返回表[hash]->getValue();
}
无效放置(int键,int值)
{
int hash=(键%TABLE_SIZE);
while(表[hash]!=NULL和表[hash]->getKey()!=key)
哈希=(哈希+1)%TABLE\u SIZE;
if(表[hash]!=NULL)
删除表[hash];
表[hash]=新的hash_项(键、值);
}
~hash_map()
{
对于(int i=0;i
您需要在构造函数中初始化它,更改

const int TABLE_SIZE = 128;

以及来自

hash_map() 
  {
  table = new hash_entry*[TABLE_SIZE];
  for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL;
  }
hash_-map()
{
table=新的散列项*[表大小];
对于(inti=0;i

hash\u map():表大小(128)
{
table=新的散列项*[表大小];
对于(inti=0;i
这就是编译错误的原因。它仅在C++11中允许,在C++03和C++98中不允许

使其成为类的
静态成员,或者在构造函数中初始化它。使用它的成员初始化列表


除此之外,不要忘记实现下面的复制语义,或者通过在
private
部分声明它们(不要定义)来完全禁用它。我认为,在这种情况下禁用它会更有意义。

您需要将初始化放入构造函数中,如下所示:

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;
      }
  ...
类哈希映射
{
私人:
散列项**表;
const int TABLE_SIZE;
公众:
哈希映射():表大小(128)
{
table=新的散列项*[表大小];
对于(inti=0;i
您可以更具体一些,并提供一个最小的示例来重现问题和编译器给您的确切错误。但无论如何,即使没有打开心理模式,这一行中的错误也很明显:

const int TABLE_SIZE = 128;
您不能像这样初始化类成员,它必须在构造函数初始化列表中完成,除非该成员是静态常量编译时表达式(
constexpr
,在C++11中)

因此,从该行中删除
=128
,并修改构造函数以执行此操作:

hash_map() : TABLE_SIZE (128)
      {
....
使用枚举类型:

class hash_map
{
  private:
    enum
    {
        TABLE_SIZE = 128;
    };
  hash_entry * table[TABLE_SIZE];
//...
};

您能提供实际的错误消息吗?在
delete
ing之前,您不需要检查NULL。
delete NULL;
是完全有效的。不要忘记实现复制构造函数和赋值运算符,或者将它们声明为私有。@Praetorian-注意到。谢谢您……谢谢……有人会认为const对编译器意味着静态er@ChrisAaker:不,如果在构造函数中初始化,则类的每个实例都可以有不同的
const int TABLE_SIZE
。@Chris:对于全局变量,它有(尽管
static
在这里有非常不同的含义).Member变量只有在声明为静态时才是静态的-有时您需要非静态常量成员。@Chris不一定,如果值在类实例中可能不同,那会怎样?嗯,我想const用于pi=3.14…常量…在所有实例中都是相同的。最好将其设置为
静态
,to减小对象大小并允许进行一些重大优化。
const int TABLE_SIZE = 128;
hash_map() : TABLE_SIZE (128)
      {
....
class hash_map
{
  private:
    enum
    {
        TABLE_SIZE = 128;
    };
  hash_entry * table[TABLE_SIZE];
//...
};