在这个模板函数中,我试图从 boost ptr_map 中检索 at 元素。为清楚起见,我省略了错误处理代码。
template <typename K, class T>
class A
{
public:
void TryGet(const K &key, T &o) { o = mObjects.at(key); }
private:
boost::ptr_map<K, T> mObjects;
};
typedef A<std::string, B> myClass;
我收到编译器错误 C2582:“operator =”函数在“B”中不可用。为什么将 mObjects.at() 的返回值赋值给引用需要访问实例化类的赋值运算符?返回此值的正确方法是什么?
最佳答案
Why does the assignment of the return value of mObjects.at() to a reference need access to an assignment operator of the instantiated class?
当您分配给一个引用时,您是在分配给该引用所引用的对象。
int i = 0;
int& iRef = i; // There is no assignment, just initializing the reference.
iRef = 10; // Same as i = 10
更新,回应 OP 的评论
您所看到的相当于:
int j = 10;
int& jRef = j;
iRef = jRef; // Same as i = j
关于c++ - 为什么会出现此错误 (C2582 : 'operator =' function is unavailable in 'B' ) when assigning to a reference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36797454/