string = 编译不通过

C++语言 码拜 8年前 (2016-09-23) 1294次浏览
一个Class里面有一个结构体,和一个这个结构体的deque;如下图
class ConfigReader
{
private:
struct KVPair
{
std::string key;
std::string key_value;
KVPair(){;}; //结构体的构造函数
KVPair(const KVPair& kv) :key(kv.key),key_value(kv.key_value){;} //结构体的构造函数2;拷贝构造函数;
//操作符重载
const KVPair& operator= (const KVPair& kv)
{
key = kv.key;
key_value = kv.key_value;
return *this;
}
bool operator==(const KVPair& kv) const { return (key==kv.key); }
bool operator<(const KVPair& kv) const {return (key_value<kv.key_value);}
};
deque<KVPair> key_value_table;
其中结构体的2个元素都是string
调用时编译无法通过;
string = 编译不通过
编译报错:
:\users\luoyang.win-a98iv7hj3bi\desktop\vstest0628\operate\operate\configreader.cpp(836): error C2678: 二进制“=”: 没有找到接受“const std::string”类型的左操作数的运算符(或没有可接受的转换);
string = string应该不会报错啊;求指导
解决方案

5

你的函数声明 this 是 const 的,所以 this->key_value_table 也是 const 的

10

const KVPair& operator= (const KVPair& kv)
改成
KVPair& operator= (const KVPair& kv)
试下

30

你这个成员函数带了const关键字就不能修改里面的成员了
void set(…)const;//这个const去掉

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明string = 编译不通过
喜欢 (0)
[1034331897@qq.com]
分享 (0)