C++ 怎么定义一个string类变量?

suuare 2004-12-28 11:23:43
新建了一个空DOS工程 写下如下代码:
#include "iostream.h"
#include "conio.h"
#include "string.h"



int main(){
string s = "Hello!";
cout<<s<<endl;
getch();
return 0;
}
保存为test1.cpp
编译后报错 :
test1.cpp
...\test1\test1.cpp(8) : error C2065: 'string' : undeclared identifier
...\test1\test1.cpp(8) : error C2146: syntax error : missing ';' before identifier 's'
...\test1\test1.cpp(8) : error C2065: 's' : undeclared identifier
...\test1\test1.cpp(8) : error C2440: '=' : cannot convert from 'char [7]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
...全文
3048 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sonic1984 2004-12-28
  • 打赏
  • 举报
回复
换个编译器试试吧 可能是你的编译器太老了 你用的什么编译器?

还有,记得加上using namespace std;哟
suuare 2004-12-28
  • 打赏
  • 举报
回复
那怎么解决?
dongyuanzhang 2004-12-28
  • 打赏
  • 举报
回复
编译器不支持而已!
suuare 2004-12-28
  • 打赏
  • 举报
回复
出现这个错误
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)
Error executing cl.exe.
string类型不能用cout输出吗?
手抓宝 2004-12-28
  • 打赏
  • 举报
回复
char arr[]=""???
「已注销」 2004-12-28
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <string>
#include <iostream.h>
using namespace std;

void main()
{
string a;
}
xuzheng318 2004-12-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;


int main(){
string s = "Hello!";
cout<<s<<endl;
getch();
return 0;
}
sonic1984 2004-12-28
  • 打赏
  • 举报
回复
我也觉得是头文件的问题

c++的头文件的没有扩展名的
moke 2004-12-28
  • 打赏
  • 举报
回复
vc6支持的呀,关键是你少了using namespace std
horisly 2004-12-28
  • 打赏
  • 举报
回复
回复人: suuare(督察) ( ) 信誉:99 2004-12-28 11:49:00 得分: 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)
Error executing cl.exe.
string类型不能用cout输出吗?


**************************************************
用string时候,要用标准的c++文件包含方式,也就是:
#include<string>
using namespace std;
否则,你用#include<string.h>的话,编译器是认为这里包含的是c里面的string头文件

---------------------------------
然后,之所以出现你上面所说的问题,是因为你用了using namespace std;
所以也要如此声明 #include<iostream> 而不是 #include<iostream.h>
------------------------------
所以正确的写法是:


回复人: sharkhuang(共享智慧) ( ) 信誉:100 2004-12-28 13:51:00 得分: 0


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




agaric 2004-12-28
  • 打赏
  • 举报
回复
恩 要用string的话 必须是 include<string>

string.h是C语言里的
「已注销」 2004-12-28
  • 打赏
  • 举报
回复
这样就可以了:
#include "stdafx.h"

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

void main()
{
string a = "afda";
cout<<a<<endl;
}
sharkhuang 2004-12-28
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
using namespace std;
suuare 2004-12-28
  • 打赏
  • 举报
回复
可能... microsoft visual C++ 1994-1998 版本是6.0 怎样升级呢?
macrod 2004-12-28
  • 打赏
  • 举报
回复
或者 std::cout

64,282

社区成员

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

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