-7

我在这里有一个简单的 C++ 程序:

#include <iostream>
using namespace std;
main ()              //no return type for main. Yet program compiles and runs ok
{                    //when run by itself.
  cout << "hi";
}

但是如果我在另一个名为的文件中添加空白单元测试,程序将不再编译newsimpletest1.cpp

#include <stdlib.h>
#include <iostream>

int main(int argc, char** argv) {

}

如果我运行它,它会按预期编译并打印“hi”。但是,如果我测试项目,我会得到一个错误:

error: ISO C++ forbids declaration of `__nomain' with no type

当我将返回类型“int”添加到“main”时,它会编译并正确运行。我试图弄清楚这个错误试图告诉我什么。

我正在使用 Windows XP 编译 Netbeans 7.1.2,使用默认的 g++ 编译器。

4

1 回答 1

2

它说,在托管环境中,main应该有一个类型。引自 C++ 标准 3.6.1,第 2 段

实现不应预定义main功能。该功能不得重载。它的返回类型应该是 type int,否则它的类型是实现定义的。所有实现都应允许 - () 的函数返回int和 - (int, 指向 char 的指针的指针) 的函数以 (8.3.5)int的类型返回。main

于 2013-09-24T17:56:58.443 回答