2017-04-10 59 views
1

问:转换无效的错误和无效的类型

写,没有实现对学生的滚动哈希函数的程序,并在他们的家庭进行分类。就像5000423,最后2位数字23,2 + 3 = 5,所以属于家族5

我尝试:捕获

#include<iostream> 
#include<cstdlib> 
#include<string> 
using namespace std; 

const int tablesize= 20; 

class hashentry 
{ 
public: 
    int key; 
    int value; 

    hashentry(int key,int value) 
    { 
     this->key=key; 
     this->value=value; 
    } 
}; 

class hashmap 
{ 
public: 
    hashentry **table; 
public: 
    hashmap() 
    { 
     int table=new hashentry *[tablesize]; 
     for(int i=0;i<tablesize;i++) 
     { 
      table[i]=NULL; 
     } 
    } 

    int hashfunc(int key) 
    { 
     return key%tablesize; 
    } 

    void insert(int key, int value) 
    { 
     int hash=hashfunc(key); 
     while(table[hash]!=NULL && table[hash]->key!=key) 
     { 
      hash=hashfunc(hash+1); 
     } 
     table[hash]=new hashentry(key,value); 
    } 
}; 

int main() 
{ 
    int key; 
    int value; 
    hashmap hash; 
    cout<<"enter value"; 
    cin>>value; 
    cout<<"enter key at which element is to be inserted"; 
    cin>>key; 
    hash.insert(key,value); 
    return 0; 
} 

错误:

In constructor 'hashmap::hashmap()': 
invalid conversion from 'hashentry**' to 'int' 
invalid types 'int[int]' for array subscript 
+0

猜测指针可能是你的程序中的一个'long' – Jay

+0

'table'应该是构造函数中的'this.table' –

+0

投票以打字方式结束。从构造函数中的int table = new hashentry * [tablesize];中移除int。 – NathanOliver

回答

1
int table=new hashentry *[tablesize]; 

的返回类型new hashentry *[tablesize]hashentry**。由于您试图将它分配给一个int变量,编译器会抱怨。很可能,因为您已经使用正确的类型定义了一个具有相同名称的成员变量,所以您打算省略int。写作

table = new hashentry *[tablesize]; 

应该这样做。

相关问题