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
  • 198,982
  • 21
  • 284
  • 478
user1198065
  • 190
  • 2
  • 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
  • 22,732
  • 9
  • 48
  • 60
  • 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
  • 37,093
  • 35
  • 149
  • 233