C++primer简单问题

swordman123 2008-06-23 09:52:32
1:
std::string s("hello world");
std::cout << s << std::endl;

2:
std::cout "hello world" <<std::endl;

1不能编译通过,2能通过;为什么1不能编译通过?

编译环境vs2008
...全文
93 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sevenlight_x 2008-06-23
  • 打赏
  • 举报
回复
#include <string>
swordman123 2008-06-23
  • 打赏
  • 举报
回复
我是楼主,手输入错了:
1:
std::string s("hello world");
std::cout << s << std::endl;

2:
std::cout << "hello world" << std::endl;
xkyx_cn 2008-06-23
  • 打赏
  • 举报
回复
要想cout输出stl::string类,可以如下表示:


std::string s("hello world");
std::cout << s.c_str() << std::endl;
xkyx_cn 2008-06-23
  • 打赏
  • 举报
回复
stl的string类没有重载<<操作符,cout继承的<<操作符里面也没有操作stl::string类型的
而2中参数类型是 const char* 类型的,cout的<<操作符有此类型的重载
sukyin 2008-06-23
  • 打赏
  • 举报
回复
你说反了吧。

64,654

社区成员

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

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