标准迭代器分配错误
Std iterator assignment error
如果这个问题很傻,请耐心等待。
头文件中定义了以下内容:
typedef char NAME_T[40];
struct NAME_MAPPING_T
{
NAME_T englishName;
NAME_T frenchName;
};
typedef std::vector<NAME_MAPPING_T> NAMES_DATABASE_T;
后来需要找到一个特定的英文名称:
const NAMES_DATABASE_T *wordsDb;
string str;
std::find_if( wordsDb->begin(),
wordsDb->end(),
[str](const NAME_MAPPING_T &m) -> bool { return strncmp(m.englishName, str.c_str(), sizeof(m.englishName)) == 0; } );
这段代码(老实说,我复制粘贴了它)可以编译,但如果我想检查find_if()返回的值,如下所示:
NAMES_DATABASE_T::iterator it;
it = std::find_if(blah ..)
代码将不会编译
实际上,这条线it=std::find_if(…)将返回错误:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_const_iterator<_Myvec>' (or there is no acceptable conversion)
怎么了?
谢谢你抽出时间。
const NAMES_DATABASE_T *wordsDb;
您的wordsDb
是const,因此wordsDb->begin()
返回一个const迭代器,所以find_if
也返回一个常量迭代器。您正试图将该常量迭代器分配给非常量NAMES_DATABASE_T::iterator it
,因此出现错误。
您可以使用NAMES_DATABASE_T::const_iterator
来获取常量迭代器。您应该使用std::string
而不是那些字符缓冲区,除非在一些罕见的情况下需要其他缓冲区。
相关文章:
- vector.resize()中的分配错误
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 如何修复只读对象中成员的错误分配?
- 无法将错误分配给成员函数中"类型的实体"
- 分段错误分配函数结果
- 如何实现,错误分配中止而不是抛出异常
- C++代码错误分配
- 如何修复错误分配C++运行时错误
- 分段错误 - 分配类困难
- 带有数组类型表达式的错误分配
- Sprintf 缓冲区问题,错误分配给字符数组
- 而 (p=strtok(NULL, "," )) 警告:p可识别的错误分配
- 错误:分配只读位置“arr2.IntArray::operator[](1)”arr2[1]=24;
- std::映射错误分配错误
- 为什么"e.what()"打印"错误分配"?
- std::vector<const char *>的错误分配效果?
- 使用指向类的指针进行错误分配
- 错误:分配只读引用“__b”
- 向量错误分配错误 C++