我试图访问Hashtable将是一个静态函数初始化()访问从静态函数
这是我的代码看起来像一个非静态成员的非静态结构。 我收到以下错误,当我运行这个
“未定义参考`哈希::哈希表'” 有什么办法,我可以访问来自初始化与哈希表中的定义相同。
class Hash
{
private:
static const int tableSize = 10;
struct item
{
string name;
item* next;
};
static item* HashTable[tableSize];
public:
static void Initialize();
static int Hash(string key);
};
----------------------------------------------------------------------------
--------------------------------hash.cpp------------------------------------
#include<iostream>
#include<string>
#include "hash.hpp"
using namespace std;
hash::Initialize()
{
for(int i=0;i<tableSize;i++)
{
HashTable[i] = new item; //Gives an error
HashTable[i]->name = "empty";//Gives an error
HashTable[i]->next = NULL;
}
}
int hash::Hash(string key)
{
int hash=0;
int index=0;
for(int i=0;i<key.length();i++)
{
hash = (hash + (int)key[i]);
}
index = hash % tableSize;
cout<<"Index-"<<index<<endl;
return index;
}
int main(int argc,char** argv)
{
Hash:Initialize();
Hash::PrintTable();
return 0;
}
查找一个定义规则是什么。静态成员需要在一个翻译单元中实例化。简单地包括头文件是声明,而不是实例化。 – yngccc