Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误C2662无法从常量转换为引用_C++_Constants_Hashtable - Fatal编程技术网

C++ 错误C2662无法从常量转换为引用

C++ 错误C2662无法从常量转换为引用,c++,constants,hashtable,C++,Constants,Hashtable,这是我关于堆栈溢出的第一篇文章,我希望将来能加入这个社区 我正在为ADT类编写一个哈希表实现;我的大多数方法在任务的范围内达到了标准,但这给了我悲伤。 在我编写各种函数时使用的这个测试应用程序中,我收到了错误“error C2662:'customer::getPhone”:无法将'this'ponter从'const customer'转换为'customer&' “cursor=find_ptr(entry.getPhone());" 及 列表头插入(数据[hash(entry.getPho

这是我关于堆栈溢出的第一篇文章,我希望将来能加入这个社区

我正在为ADT类编写一个哈希表实现;我的大多数方法在任务的范围内达到了标准,但这给了我悲伤。 在我编写各种函数时使用的这个测试应用程序中,我收到了错误“error C2662:'customer::getPhone”:无法将'this'ponter从'const customer'转换为'customer&' “cursor=find_ptr(entry.getPhone());" 及 列表头插入(数据[hash(entry.getPhone())],entry);"

我的函数代码实现如下:

    template <class RecordType>
void table<RecordType>::insert(const RecordType& entry){

    node<RecordType>* cursor;
    cursor = find_ptr(entry.getPhone());

    if(cursor == NULL) {
        list_head_insert(data[hash(entry.getPhone())], entry);
        ++total_records;
    }
    else
        cursor->set_data(entry);

}    
模板
空表::插入(常量记录类型和条目){
节点*光标;
cursor=find_ptr(entry.getPhone());
if(游标==NULL){
列表头插入(数据[hash(entry.getPhone())],entry);
++记录总数;
}
其他的
光标->设置数据(输入);
}    
在本例中,getPhone引用整数私有变量的访问器,没有什么特别之处

最后,对于我的主要测试应用程序:

#include "Table2.h"
#include "Customer.h";
using namespace std;

int main () {

    customer myCustomer( "name", "935 street dr.", 5555555 );
    table<customer> myTable;
    cout << myCustomer;

    myTable.insert(myCustomer);


    return 0;
}
#包括“表2.h”
#包括“Customer.h”;
使用名称空间std;
int main(){
客户我的客户(“姓名”,“935街医生”,555);
表myTable;

cout访问器
RecordType::getPhone()
需要声明
const

这纯粹是我的语法错误。在我睡眠不足的愚蠢中,我键入:

Const int getPhone() {return customerPhoneNumber;}; 
而不是

int getPhone() const {return customerPhoneNumber;};

愚蠢

奇怪的是,我尝试将访问器编码为常量,但仍然得到相同的结果。const int getPhone(){return customerPhoneNumber;};您需要将
const
放在函数后面,如上面的答案所示。