我正在尝试创建一个哈希表解释器,遇到一些错误。
每次使用hashString时都会收到此错误。
main.cpp:99:43: error: use of undeclared identifier 'hashString'
temp.next = table[hashString(temp.getName())];
^
main.cpp:100:31: error: use of undeclared identifier 'hashString'
table[hashString(temp.getName())] = temp;
我尝试声明hashstring(已注释掉)但它只是给了我更多这样的错误:
main.cpp:105:34: error: no viable overloaded operator[] for type 'node [7]'
任何建议都表示赞赏。谢谢!
我的代码:
class node{
public:
node();
node* next;
string getName();
int getNum();
int getScope();
void setName(string);
void setNum(int);
void setScope(int);
int hashString(string s);
private:
string name;
int num;
int scope;
};
node::node(){
next = 0;
name = "";
num = 0;
scope = 0;
}
string node::getName(){
return name;
}
int node::getNum(){
return num;
}
int node::getScope(){
return scope;
}
void node::setName(string s){
name = s;
}
void node::setScope(int x){
scope = x;
}
void node::setNum(int x){
num = x;
}
int node::hashString(string s){
int hash=0,size = s.size();
for(int i= 0;i<size+1;i++){
hash+= (int) s[i] * i;
}
return hash%7;
}
int main(){
int curr_scope=0,num_line=0;
node table[7];
//node hashString(string s);
ifstream myfile ("input.txt");
if (myfile.is_open()){
string s;
while (getline (myfile,s)){
num_line++;
stringstream line(s);
line >> s;
if(s == "START"){
curr_scope++;
while(line >> s);
}
else if(s == "FINISH"){
curr_scope--;
while(line >> s);
}
else if(s == "COM"){
while(line >> s);
}
else if(s == "VAR"){
node temp;
line >> s;
cout << s << endl;
temp.setName(s);
line >> s;
if(s == "="){
line >> s;
cout << s << endl;
temp.setNum(atoi(s.c_str()));
temp.setScope(curr_scope);
if(table[hashString(temp.getName())] != 0){
temp.next = table[hashString(temp.getName())];
table[hashString(temp.getName())] = temp;
while(line >> s);
}
else if(table[hashString(temp.getName())] == 0){
table[hashString(temp.getName())] = temp;
while(line >> s);
}
else{
cout << "UNABLE TO ADD " << temp.getName() << "TO THE TABLE" << endl;
while(line >> s);
}
}
}
else if(s == "PRINT")
{
line >> s;
node temp = table[hashString(s)];
if(temp.getScope() == curr_scope){
if(line >> s){
if(s == "++"){
cout << temp.getName() << " IS " << temp.getNum() + 1 << endl;
while(line >> s);
}
else if(s == "--"){
cout << temp.getName() << " IS " << temp.getNum() - 1 << endl;
while(line >> s);
}
else if(s == "+"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() + atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "-"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() - atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "/"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() / atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "*"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() * atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "%"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() % atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "^"){
line >> s;
cout << temp.getName() << " IS " << pow(temp.getNum(),atoi(s.c_str())) << endl;
while(line >> s);
}
}
}
else{
cout << s << "IS UNDEFINED" << endl;
cout << "ERROR HAS OCCURED ON LINE " << num_line << endl;
while(line >> s);
}
}
else{
if(table[hashString(s)].getName == s){
node temp = table[hashString(s)];
line >> s;
if(s == "="){
if(temp.getScope() == curr_scope){
line >> s;
table[hashString(temp)].setNum(atoi(s.c_str()));
while(line >> s);
}
}
else if(s == "++"){
table[hashString(temp)].setNum(table[hashString(temp)].getNum()+1);
while(line >> s);
}
else if(s == "--"){
table[hashString(temp)].setNum(table[hashString(temp)].getNum()-1);
while(line >> s);
}
}
else
cout << s << "IS UNDEFINED" << endl;
}
}
myfile.close();
}
system("PAUSE");
return 0;
}
答案 0 :(得分:0)
为什么hashString
是Node的一部分?
如果没有理由,那么您应该将其移至免费功能,这将解决您的问题。
否则,您需要在node
实例上调用它,例如temp.hashString(s)
。
你也可以把它变成一个类静态函数,这样你可以像这样调用它:node::hashString(s)
如果你想让它与节点类相关联。