错误:
invalid initialization of reference of type
Assoc<float, std::basic_string<char> >&
from expression of typeconst Assoc<float, std::basic_string<char> >
对于这段代码
Assoc<keyType,valueType>& found = internalStorage.get(find(key));//returns the value of some key
对不起你们,我知道这没意思,但我很困惑。
知道问题出在哪里吗?
最佳答案
看起来像internalStorage.get()
按值返回对象,而您正试图绑定(bind)一个非 const
对返回的临时文件的引用。
解决此问题的最佳方法取决于您要执行的操作(以及 internalStorage
的类型):
- 如果不需要修改
found
, 引用const
(参见 Does a const reference prolong the life of a temporary?)。 - 如果确实需要修改
found
(这是存储在internalStorage
中的拷贝),只需删除&
. - 如果需要修改存储在
internalStorage
中的对象,您可能需要重构代码。
关于c++ - 错误 : invalid initialization for struct in C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088571/