1、错误提示:
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)
2、错误原因:
#include<iostream.h>
#include<string>
using namespace std;
在#include<iostream.h>中string类没有重载“<<”操作符
3、错误代码:
string str=" ";
cout<<str<<endl;
4、解决办法(string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址;
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.)
string str=" ";
cout<<str.c_str()<<endl;
本文介绍了在使用C++进行编程时遇到的一个常见问题:如何正确地将string类型的数据输出到控制台。文章详细解释了错误发生的原因,并提供了一个简单的解决方案,即通过调用c_str()函数来转换string以便于输出。
3万+

被折叠的 条评论
为什么被折叠?



