回 帖 发 新 帖 刷新版面

主题:有个小小的错误,不知怎么改

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion
)

这是个什么错误啊?就是按照书上输入的应该没有错误才对啊。

还有下面第一行#pragma warning (disable:4786)是什么意思啊?刚开始没有加这个时,总是有错误,后来看了别人程序,加上之后,就可以了。


#pragma warning (disable:4786)
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;

int main()

{
    
    map<int, string> mapStudent;
    
    mapStudent.insert(map<int, string>::value_type (1, "student_one"));
    
    mapStudent.insert(map<int, string>::value_type (2, "student_two"));
    
    mapStudent.insert(map<int, string>::value_type (3, "student_three"));
    
    map<int, string>::iterator  iter;
    
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
        
    {
        
        cout<<iter->first;
        cout<<""<<iter->second;
        
    }
    return 0; 
}

回复列表 (共4个回复)

沙发

1、为了解决这个错误,只需要:#include <string>

2、#pragma warning (disable:4786)
这个是Visual C++ 6.0的“特色”了。
VC6在调试时,允许一个符号的长度最多只能有255个字符。但是map<int, string>的实际长度会超出255的限制。(原因:string其实是'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'的typedef,而map是一个名字更长的类型的typedef,所以map<int, string>很容易超过255)
一旦超过255的限制,就会产生第4786号告警。而#pragma warning (disable:4786)就是让编译器不要发出这个告警信息。

板凳

[quote]1、为了解决这个错误,只需要:#include <string>

2、#pragma warning (disable:4786)
这个是Visual C++ 6.0的“特色”了。
VC6在调试时,允许一个符号的长度最多只能有255个字符。但是map<int, string>的实际长度会超出255的限制。(原因:string其实是'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'的typedef,而map是一个名字更长的类型的typedef,所以map<int, string>很容易超过255)
一旦超过255的限制,就会产生第4786号告警。而#pragma warning (disable:4786)就是让编译器不要发出这个告警信息。[/quote]
 &#22826;&#24863;&#35874;&#20102;&#65292;&#20320;&#22238;&#31572;&#30340;&#36825;&#20040;&#23436;&#32654;&#65292;&#21035;&#20154;&#37117;&#19981;&#29992;&#22238;&#31572;&#20102;&#21704;&#21704;&#12290;

3 楼

&#22826;&#24863;&#35874;&#20102;&#65292;&#20320;&#22238;&#31572;&#30340;&#36825;&#20040;&#23436;&#32654;&#65292;&#21035;&#20154;&#37117;&#19981;&#29992;&#22238;&#31572;&#20102;&#21704;&#21704;&#12290;



太感谢了,你回答的这么完美,别人都不用回答了哈哈。

4 楼

完美還不結貼順便給個50分?

我来回复

您尚未登录,请登录后再回复。点此登录或注册