1

The following code compiles. But if I write the code to call the method test using jar it is giving me a compilation error. What is really happening here.

#include <iostream>    

using namespace std;

class A {
public:
  void test() {
    cout << "working" << endl;
  }
};

int main() {
  A foo;
  A jar();
}
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
user1198065
  • 210
  • 3
  • 10

2 Answers2

7
 A jar();

declares a function named jar without any parameters, and return type is object of class A.

You cannot declare a function inside main, therefore, you got the error.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • 2
    It's not really the MVP (`SomeType var(SomeClass());`), but close enough. You can declare the function, but you can't use it like an object. – chris Apr 01 '13 at 22:30
1

If you tried to declare a function pointer named ptr you should declare it like that:

A (*ptr)(void);
0x90
  • 39,472
  • 36
  • 165
  • 245