0
#include<bits/stdc++.h>
using namespace std;

class A{
    int x;

public:
    A(){
        x=10;
    }

    void show(){
        cout<<x<<endl;
    }
};

main(){

    A a;
    a.show();

}

In the main() function when I am declaring the variable a in the above way, the code works fine but if we declare the variable A a() compiler gives error. Why is it so? I think there is no problem regarding argument type matching. Can anyone help?

biswas N
  • 381
  • 1
  • 16

2 Answers2

1

Because A a() is not a variable declaration but a function prototype declaration.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

C++11 and above fix this problem with universal initializers. You are declaring a function that returns an A. If you instead use {} it will work:

A a{};
Edward Strange
  • 40,307
  • 7
  • 73
  • 125