c++ - 为什么会出现此错误 (C2582 : 'operator =' function is unavailable in 'B' ) when assigning to a reference?

标签 c++ boost

在这个模板函数中,我试图从 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/

相关文章:

c++ - 修改 Windows 磁盘驱动程序以使用更新的控制代码,如 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX

c++ - 实例化 COM 对象时出错

c++ - C++ 或 C 中函数的名称

c++ - 如何静态编译 boost_iostreams?

c++ - boost.asio - 设置最大读取流大小

c++ - boost::property_tree xml pretty-print 、格式化

c++ - 无法在 Mac 终端中运行 qmake

c++ - 在 WScript.CreateObject 中使用前缀的目的是什么?

c++ - 将依赖于对象的比较器作为模板参数传递,

c++ - boost::bind 函数及其输入参数错误