1
#include <iostream>
using namespace std;

class A {
    public:
        int x;
    A() {
        x=1;
        cout << x <<endl;
    }
};

int main()
{
    A b();
    cout << b.x << endl;
    return 0;
}

Running this results in compilation error: Error: request for member 'x' in 'b', which is of non-class type 'A()' "cout<< b.x <

  • You declared the object but never created it. Try to create it with the 'new' keyword. A b = new b(); In your case it is like a function definition. – Karavana Dec 01 '18 at 13:50
  • Google "c++ most vexing parse" to find out what the compiler really did with that code. Drop the parentheses. – Hans Passant Dec 01 '18 at 13:52
  • This is the C++ most vexing parse. `A b();` declares a function named `b()` that accepts no arguments and returns an `A`. It does not declare a variable. – Peter Dec 01 '18 at 14:02
  • @Karavana - your suggestion is not valid in C++. – Peter Dec 01 '18 at 14:04
  • @Karavana he is not using pointers so the new keyword would not fix this code. A b = A(); would actually be more valid – Will Anderson Dec 01 '18 at 14:34
  • 1
    @WillAnderson you'r right, in my comment it shoulda been A* b = new A(); or as you said, A b = A(); – Karavana Dec 05 '18 at 20:24

0 Answers0