取消引用字符串迭代器无法编译
Dereferencing string iterator won't compile
我遇到了一个问题,示例代码将在代码块环境中编译和运行,但不会在visual studio 2012 中编译
list<string> names;
names.push_back("Mary");
names.push_back("Zach");
names.push_back("Elizabeth");
list<string>::iterator iter = names.begin();
while (iter != names.end()) {
cout << *iter << endl; // This dereference causes compile error C2679
++iter;
}
导致以下编译器错误
1>chapter_a0602.cpp(20): error C2679: binary '<<' : no operator found which takes a
right-hand operand of type 'std::basic_string<_Elem,_Traits,_Alloc>' (or there is no
acceptable conversion)
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
当我将字符串列表更改为int列表时,代码将在VS2012中编译并运行。
当我也将取消引用更改为以下内容时,它会编译
cout << *the_iter->c_str() << endl;
然而,在代码的后面,我还有另外两个取消引用的问题
cout << "first item: " << names.front() << endl;
cout << "last item: " << names.back() << endl;
我真的不明白为什么这些错误依赖于编译器。
很抱歉格式化,但我无法让它接受代码。
添加以下include指令:
#include <string>
因为这是定义CCD_ 1的地方。
注意VS2012支持基于范围的for语句,它将把输出循环转换为:
for (auto const& name: names) std::cout << name << std::endl;
ostream operator<<(ostream& os, const string& str)
在string
标头中定义。
你可能只是忘了把它包括在内,才会出现这种错误。
你应该把它放在文件的顶部:
#include <string>
实时示例。
相关文章:
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 编译错误 std::vector<std::shared_ptr<T>>迭代器和擦除方法
- 如果我在下面的代码中使用 list 而不是 vector,为什么在我尝试在迭代器之间执行减法的行中编译失败?
- C++向量迭代器nth_element编译错误
- 为什么 std::variant 使用开始和结束迭代器进行编译?
- 在将迭代器与C 中的null进行比较时,编译误差
- 编译错误与迭代器取消引用的 decltype
- 使用set<set>迭代器的编译错误<int>
- 通过迭代器调用功能无法在派生对象上编译
- C :多态容器 /迭代器与编译时间概念 /特征
- 迭代器使用的编译时验证
- 从函数返回 ::迭代器不会编译
- 二叉树编译错误的迭代器
- C++:使用clang编译以迭代器为值的映射时出现巨大错误
- C++ - 未编译的对象向量的迭代器
- 编译错误 ::向量 ::迭代器
- 链表迭代器模板中的编译错误
- 取消引用字符串迭代器无法编译
- Clang 3.0 C++ std::map<>::迭代器编译错误
- 使用映射迭代器编译错误