VC++6.0 为什么不能用string??

#include<string.h>
#include<iostream.h>
void main()
{
string s("try a string test");
cout<<s<<endl;
}
编译错误error C2065: 'string' : undeclared identifier

把下列头文件都加上,就好了

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

void main()
{
string s("try a string test");
cout<<s<<endl;
}

 

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-07
// 1、 C++ 的头文件是不需要 .h 后缀的
// 2、 请指明 C++ 标准命名空间 std
#include <iostream>
#include <string>
using namespace std;
void main()
{
string s("try a string test");
cout<<s<<endl;
}


第2个回答  推荐于2017-09-17

用标准的写法即可.不过建议用新点的vc.如2005及以上的.或直接用gcc,对标准的支持会好些.

#include<string>
#include<iostream>
using namespace std;
int main()
{
string s("try a string test");
cout<<s<<endl;
return 0;
}

本回答被提问者采纳
第3个回答  2013-10-07
改成#include<string>
“string.h“这个头文件是“旧式c头文件”,而这个文件中没有定义string类(这点应该不奇怪,c语言中哪有什么类啊),这个头文件里面是有关“旧式char-based字符串”的操作函数,注意都是操作char*字符串的“函数”,所以你引用这个头文件,编译器肯定找不到“string”了。
第4个回答  2013-10-07
#include<string>
#include<iostream.h>
void main()
{
char s[]="try a string test";
cout<<s<<endl;
}
=。=老兄 我也只能弄成这样了 你看看吧
相似回答