hackrock
hackrock

Reputation: 317

Why is compiler not flagging this as an Error instead of warning?

#include <iostream>

using namespace std;

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

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

} 

Output:

HERE

Compiler skips the line "test testObj(); " and compiles the rest with warning and when run will generate the output. The warning is "prototyped function not called (was a variable definition intended?) in VC++ 2008. Why does it not throw an error?

Upvotes: 1

Views: 124

Answers (3)

Adel Boutros
Adel Boutros

Reputation: 10285

Remove the () from the constructor call in Main

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

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 791361

Simply, because it's not an error to declare a function such as the one you declared. The warning should be useful enough, though.

Upvotes: 3

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272447

Because it's not an error.

Your code has fallen foul of the most-vexing parse (in summary, test testObj(); doesn't define a variable, it declares a function).

Upvotes: 8

Related Questions