为什么用VC6编译带bool变量的代码时会报错?

chg_lele 2008-02-11 02:10:39
想学C++,找了点代码看并编译,发现VC6下会报错,不知道为什么?谢谢帮忙!
并请告知如何解决?


#include <iostream.h>
// #include <iostream>

/**
**
the while loop has executed 1 times
the while loop has executed 2 times
the while loop has executed 3 times
the while loop has executed 4 times
the while loop has executed 5 times
**
**/
int main()
{
int iterations = 0;
bool continue_loop = true;
while ( continue_loop != false )
{
iterations++;

cout << "the while loop has executed "
<< iterations << " times\n";

if ( iterations == 5 )
continue_loop = false;
}

return 0;
}


--------------------Configuration: alg0 - Win32 Debug--------------------
Compiling...
alg0.c
E:\Chg\mine\CC++\Code\alg0.c(16) : error C2065: 'bool' : undeclared identifier
E:\Chg\mine\CC++\Code\alg0.c(16) : error C2146: syntax error : missing ';' before identifier 'continue_loop'
E:\Chg\mine\CC++\Code\alg0.c(16) : error C2065: 'continue_loop' : undeclared identifier
E:\Chg\mine\CC++\Code\alg0.c(16) : error C2065: 'true' : undeclared identifier
E:\Chg\mine\CC++\Code\alg0.c(18) : error C2065: 'false' : undeclared identifier
E:\Chg\mine\CC++\Code\alg0.c(22) : error C2065: 'cout' : undeclared identifier
E:\Chg\mine\CC++\Code\alg0.c(23) : error C2297: '<<' : illegal, right operand has type 'char [29]'
Error executing cl.exe.

alg0.exe - 7 error(s), 0 warning(s)

...全文
519 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljpdb 2012-08-12
  • 打赏
  • 举报
回复
加个<windows.h>就行
arong1234 2008-02-12
  • 打赏
  • 举报
回复
4楼:首先C支持的东西是少于C++的,C++有的,在C中部一定有,如bool在C中就没有
另外:cin/cout都是类对象,类在C中没有,所对应的对象cin/cout自然也不支持
至于为什么包含头文件:类型都定义在头文件里,不包含你到哪找?
using namespace std就是避免你写std::cin std::cout
arong1234 2008-02-12
  • 打赏
  • 举报
回复
5楼:很多编译器是根据扩展名自适应选择用C还是C++编译的,VC缺省作这种选择
visame 2008-02-12
  • 打赏
  • 举报
回复
A_B_C_ABC好样的!
过去的我 2008-02-11
  • 打赏
  • 举报
回复
为什么要把文件名改成.cpp ,我觉得编译器需要的只是个源文件,不看后缀的吧
bool ,编译器说 undeclared identifier,应该是缺了个头文件,其他好多vc变量都是宏定义,加个<windows.h>就行了
还是vc6的编译期和标准c++有不同,所以你要使学c++的话还是用别的ide吧,如mingw development studio,对中文支持似乎不大好,有时候用gdb会出错
namespace 还是看看书吧,里面解释比较专业..
chg_lele 2008-02-11
  • 打赏
  • 举报
回复
虽然说cout是在C++之后出现的,但我用VC编译C文件的时候,只要包含头<iostream>,就可以正常运行cout,cin等。
为什么C++的bool类型就不能使用啊?
另外还有string类型,为什么必须除了包含后文件<string>,还要加上using namespace std;才能使用啊?诸多疑惑还望大家帮忙!
arong1234 2008-02-11
  • 打赏
  • 举报
回复
bool式C++才有的类型吧
而且C哪支持<<和cout?
chg_lele 2008-02-11
  • 打赏
  • 举报
回复
谢谢A_B_C_ABC,已经OK了。我想问一下,难道C不支持bool型么?还是由于VC6支持的C标准版本低的原因造成的啊!再次感谢!
0黄瓜0 2008-02-11
  • 打赏
  • 举报
回复
文件扩展名改为CPP

64,282

社区成员

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

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