C++初学者,请教一个简单的问题。

ashy999 2003-11-27 02:34:55
#include<iostream.h>
#include<string.h>

void main()
{
string str("abc");
cout<<str;
}
我在VC++6.0环境下编译时报错,"string"没有声明。可是我看书上就是这末写的。
请高手帮帮忙。
...全文
42 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
czjcsdn 2003-11-30
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<string>
using std::string;

void main()
{

string s1 = "abc";
cout << s1.c_str() << endl;
//还可以类似这样用
const char *s2;
s2 = "def";
s1 = s1 + s2;
cout<<s1.c_str()<<endl;

}
xstarfox 2003-11-29
  • 打赏
  • 举报
回复
哇~~这个问题也困绕了我好久
今天却突然看到这个帖子,真是多谢拉~~!
minghui000 2003-11-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str("abc");
cout<<str;
}
ashy999 2003-11-28
  • 打赏
  • 举报
回复
明白了,非常感谢诸位高手,以后遇到问题还会向大家请教的,谢谢!
heguobaoceo 2003-11-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str("abc");
cout<<str;
}

这样应该没有问题了啊,因为在库中可以找到的不用.H文件
如果是你自己定义的话应该是“set.h"这样的形式
glacierrr 2003-11-27
  • 打赏
  • 举报
回复
#include <string>
using namespace std;试新版的

#include<iostream.h>
#include<string.h>
已经过时了
SebastianXP 2003-11-27
  • 打赏
  • 举报
回复
using namespace std才是关键啊,建议看看Essential C++ 或是 C++ Primer
ashy999 2003-11-27
  • 打赏
  • 举报
回复
明白了,头文件不加".h"。
ashy999 2003-11-27
  • 打赏
  • 举报
回复
非常感谢psbeond!
我试了一下,还是不行。
E:\程序\VC\piont\piont.cpp(4) : error C2871: 'std' : does not exist or is not a namespace
E:\程序\VC\piont\piont.cpp(9) : error C2065: 'string' : undeclared identifier

Why?
psbeond 2003-11-27
  • 打赏
  • 举报
回复
#include <string>
using namespace std;

或:
#include <string>
...
std::cout<<str;

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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