鱼C论坛

 找回密码
 立即注册
查看: 3353|回复: 2

VC中头文件的顺序会导致编译错误么?

[复制链接]
发表于 2012-1-4 13:36:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1.     #include <stdafx.h>
  2.     #include <iostream>

  3.     void main()
  4.     {
  5.           int i;
  6.           std::cin >> i;
  7.           while(i != 42)
  8.           {
  9.                 std::cout << i <<std::endl;
  10.                 std::cin >> i;
  11.           }
  12.     }

  13. 这样就编译通过,但是要是把#include <iostream>放到上面就不行了

  14.     #include <iostream>
  15.     #include <stdafx.h>


  16.     void main()
  17.     {
  18.           int i;
  19.           std::cin >> i;
  20.           while(i != 42)
  21.           {
  22.                 std::cout << i <<std::endl;
  23.                 std::cin >> i;
  24.           }
  25.     }

  26.     Compiling...
  27.     1221.cpp
  28.     E:\C++代码\1221\1221.cpp(8) : error C2653: 'std' : is not a class or namespace name
  29.     E:\C++代码\1221\1221.cpp(8) : error C2065: 'cin' : undeclared identifier
  30.     E:\C++代码\1221\1221.cpp(8) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
  31.     E:\C++代码\1221\1221.cpp(11) : error C2653: 'std' : is not a class or namespace name
  32.     E:\C++代码\1221\1221.cpp(11) : error C2065: 'cout' : undeclared identifier
  33.     E:\C++代码\1221\1221.cpp(11) : error C2653: 'std' : is not a class or namespace name
  34.     E:\C++代码\1221\1221.cpp(11) : error C2065: 'endl' : undeclared identifier
  35.     E:\C++代码\1221\1221.cpp(11) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
  36.     E:\C++代码\1221\1221.cpp(12) : error C2653: 'std' : is not a class or namespace name
  37.     E:\C++代码\1221\1221.cpp(12) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
  38.     执行 cl.exe 时出错.
  39.     Creating browse info file...

  40.     错误代码如上
复制代码

复制代码
为什么会这样
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-4 17:27:36 | 显示全部楼层
涉及“预编译”的概念,你可以baidu搜索该问题。
不过解决的简单办法就是:把#include <stdafx.h>放在第一个#include 的位置
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-1-4 18:58:58 | 显示全部楼层

我的理解是编译器从文件头开始往下编译。按理说mian函数在#include <iostream>后面,std应该已经被预编译过了。
但事实上你#include <iostream>放第一个时编译会报错。

百度一下<stdafx.h>,作用是预先编译一些标准头文件,加快编译速度而已
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-10 16:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表