为什么编译器没有将此标记为错误而不是警告?

hac*_*ock 1 c++ visual-studio-2008 visual-c++

#include <iostream>

using namespace std;

class test{
public:
    test() { cout<<"CTOR"<<endl; }
    ~test() { cout<<"DTOR"<<endl; }
};

int main()
{
 test testObj();
 cout<<"HERE"<<endl;

} 
Run Code Online (Sandbox Code Playgroud)

输出:

HERE
Run Code Online (Sandbox Code Playgroud)

编译器跳过"test testObj();"行并用警告编译其余部分,并在运行时生成输出.警告是"VC++ 2008中没有调用原型函数(是一个变量定义?).为什么不抛出错误?

Oli*_*rth 8

因为这不是错误.

你的代码已经违背了最令人烦恼的解析(总之,test testObj();没有定义变量,它声明了一个函数).

  • 这并不是你链接到的页面中描述的那种令人烦恼的解析.那将是`test testObj(test());`. (2认同)