【发布时间】:2020-02-21 04:24:20
【问题描述】:
我想使用 [] 运算符获取和设置测试类对象的值。但是得到以下错误:
lvalue required as left operand of assignment
提前致谢。
class Candidate {
public:
Candidate(string name):
name(name) {}
string name;
};
template<typename _key, typename _value>
class Test {
_key key;
_value value;
public:
_value operator[](_key key) {
return this->value;
}
};
int main() {
Test<string, Candidate*> test;
test["something"] = new Candidate("name");
Candidate* candidate = test["something"];
return 0;
}
【问题讨论】:
-
相关:this answer 在标题“数组下标”下。
-
请不要将问题标记为重复,除非您要将其标记为与实际问题重复的问题。一般参考问题不是此问题的重复。