C++ string问题

ArdourCareful 2012-08-28 02:49:02

#include "iostream"
#include "cstring"
using namespace std;

int main()
{
string str = "switch";
cout <<str <<endl;
return 0;
}


编译报错: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)

难道VC6.0中cout函数不支持string类型的输出
...全文
180 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Isnis-fallen 2012-08-29
  • 打赏
  • 举报
回复
没错, 支持
liubing303 2012-08-29
  • 打赏
  • 举报
回复
头文件写错了 应该是#include <string>
dhdahai 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

哪头文件string和csring有什么区别,如何选择用哪个.
[/Quote]

STL的string头文件就是

#include <string>

如何选择?没得选择,就是这个
ArdourCareful 2012-08-29
  • 打赏
  • 举报
回复
哪头文件string和csring有什么区别,如何选择用哪个.
xiaoqiao_82 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
引用 5 楼 的回复:
包含的头文件错了:

C/C++ code

#include <string> //std::string

而不是

C/C++ code


#include <cstring> //包含c的字符串处理函数, 比如strlen


1.改成string问题解决,哪么该如何选择用哪个头文件?
2.我用cstring头文件时,改用print……
[/Quote]

printf("%s", str.c_str());
xiaoqiao_82 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
引用 5 楼 的回复:
包含的头文件错了:

C/C++ code

#include <string> //std::string

而不是

C/C++ code


#include <cstring> //包含c的字符串处理函数, 比如strlen


1.改成string问题解决,哪么该如何选择用哪个头文件?
2.我用cstring头文件时,改用print……
[/Quote]

printf("%s", str.c_str());
moon2lyna 2012-08-28
  • 打赏
  • 举报
回复
太迟进来了,原因都给楼上说了。头文件打错了而已,纠正一下就好了。
backbin 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
#include <iostream>
#include <string>
[/Quote]
++++++++++++++++++++++1111111111111111
liaoqi127 2012-08-28
  • 打赏
  • 举报
回复
+1[Quote=引用 4 楼 的回复:]

cout <<str.c_str() <<endl;
[/Quote]
ArdourCareful 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
包含的头文件错了:

C/C++ code

#include <string> //std::string

而不是

C/C++ code


#include <cstring> //包含c的字符串处理函数, 比如strlen
[/Quote]

1.改成string问题解决,哪么该如何选择用哪个头文件?
2.我用cstring头文件时,改用printf函数输出时能编译过,
为什么执行时内存报错?
mujiok2003 2012-08-28
  • 打赏
  • 举报
回复
包含的头文件错了:
#include <string> //std::string 

而不是

#include <cstring> //包含c的字符串处理函数, 比如strlen
liuintermilan 2012-08-28
  • 打赏
  • 举报
回复
错了
#include <iostream>
#include <string>
c_losed 2012-08-28
  • 打赏
  • 举报
回复
cout <<str.c_str() <<endl;
liuintermilan 2012-08-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstring>
IVERS0N 2012-08-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧