如何用VC环境编译C++程序?

hahahawk 2003-04-11 03:41:00
如何用VC环境编译C++程序?
我把msdn中一段standard c++程序如下:
#include <string>
#include <iostream>

void main()
{
string s1;
cout << "Enter a sentence (use <space> as the delimiter): ";
getline(cin,s1, ' ');
cout << "You entered: " << s1 << endl;;
}
在VC环境中编译,报错:
c:\wuq\cpp\mycdll\mycdll.cpp(9): error C2065: “string” : 未声明的标识符

请问既然包含了头文件string,(编译时候也没要报找不到该文件,该文件确实存在)
为何还找不到该类型?
...全文
37 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsgfgs 2003-04-11
  • 打赏
  • 举报
回复
头文件一般不用
using namespace std;
最好用
using std::string;之类的说明单独引入...
或不引入而在cpp中引入
hahahawk 2003-04-11
  • 打赏
  • 举报
回复
谢谢诸位。然来是名称空间的原因。我还以为只有.net和java中才有这个呢。
maxcai 2003-04-11
  • 打赏
  • 举报
回复
c++里面将许多好东西放在名字空间(namespace)std里所以要加上using namespace std;
workholly 2003-04-11
  • 打赏
  • 举报
回复
建议vc.net下编译。
1.新建 托管的C++空项目。
2.添加C++源文件。
帮你编译了一下。
要解决,或者如楼上几位所说,这样定义头文件:
#include <string>
#include <iostream>
using namespace std;
或者再有问题之处之前加std::,像这样:
std::string s1;
std::cout << "Enter a sentence (use <space> as the delimiter): ";
......以后你自己考虑加。
tryboy 2003-04-11
  • 打赏
  • 举报
回复
#include <string.h>
#include <iostream.h>

void main()
{
string s1;
cout << "Enter a sentence (use <space> as the delimiter): ";
getline(cin,s1, ' ');
cout << "You entered: " << s1 << endl;;
}
再试一试吧.
pooryaya 2003-04-11
  • 打赏
  • 举报
回复
包含头文件的时候还是习惯把.h加上吧!~

能为你免去一些不必要的麻烦!~
billy_seamans 2003-04-11
  • 打赏
  • 举报
回复
标准C++程序是不要在include后面加上.h后缀的,
但是,需要加上一行:
using namespace std;
这是命名空间所必需的,在C++ primer上的开头有说明的
billy_seamans 2003-04-11
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
注意,在此处加上 using namespace std;
void main()


或者,在包含头文件的时候加上.h,
#include <string.h>
#include <iostream.h>
这时候千万不要加上using namespace std;
arfi 2003-04-11
  • 打赏
  • 举报
回复
试一下改为string.h,这可能涉及到了什么命名空间的问题,我从没用过这些,每次包含头文件时我都把.h写上的。

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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