0

我有以下代码

#include <iostream>

using namespace std;

void WaitForEnter()
{
    while(1)
    {
        if('\n' == getchar()) 
        {
            break;
        }
    }
    return;
}

int main()
{
    cout<< "Press Enter to Exit... ";
    WaitForEnter();
}

这在 Microsoft Visual C++ 2010 Express 上编译并符合我的预期。在使用 code::blocks 和 gcc++ 4.7 的 Ubuntu 上,构建失败并显示以下内容error: 'getchar' was not declared in this scope. 如果我添加该行#include "stdio.h",程序将编译并以预期的行为运行。为什么这个程序stdio.h在 Ubuntu 上使用 MVC++ 2010 Express 编译,但没有使用 gcc++ 4.7 的 code::blocks。

4

3 回答 3

4

使用 MSVC,<stdio.h>作为包含<iostream>. 查看预处理的输出,或按照 MSVC 文件中的 #include 路径。

于 2012-05-21T02:13:29.753 回答
2

最简单的答案是该标准允许任何标准标头包含任何其他标头。另一方面,如果你想编写可移植的代码,你不应该依赖它,并且应该包含翻译单元所需的所有标题。

于 2012-05-21T02:21:46.537 回答
0

在 Visual Studio 中,当您创建一个新项目时,它会为您包含 stdafx.h。在这个文件中,它包括:

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
于 2012-05-21T02:09:45.997 回答