2013-08-21 25 views
2

我有一個問題,即示例代碼將編譯並在代碼塊的環境中運行,但在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>   ] 

當我將字符串列表更改爲整數列表時,代碼將在VS2012中編譯並運行。

時,我也改變了反引用它編譯

cout << *the_iter->c_str() << endl; 

不過,我還具有另外兩個非關聯問題下來的代碼

cout << "first item: " << names.front() << endl; 
cout << "last item: " << names.back() << endl; 

我真的不明白爲什麼這些錯誤有以下幾種編譯器依賴。

抱歉格式化,但我無法讓它接受代碼。

回答