+ 2

Why compiler is giving error in the second code and not in first one ?

//First code class A { public: int A; }; int main() { A o; o.A=5; cout<<o.A; return 0; } //Second code class A { public: int A; A(){} }; int main() { A o; o.A=5; cout<<o.A; return 0; } /* Note : the variable and class has same name in the both cases but compiler is giving error only when we putting a constructor in the class A */

24th May 2019, 9:08 AM
Sp Maurya
Sp Maurya - avatar
2 odpowiedzi
+ 3
The constructor has the same name as the class which is A. But you can't have two members with the same name so the integer called A conflicts with the constructor called A as well. Solution: rename the integer
24th May 2019, 9:13 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
Oh so the problem is with constructor and variable ok i got it....
24th May 2019, 9:15 AM
Sp Maurya
Sp Maurya - avatar