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