1

I tried playing around with OOP which made me write the following code :

#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class A
{
int a;
public:
A()
{
    cout << "implicit constr\n";
    a = 0;
}
A(const int& x)
{
    a = x;
    cout << "parameter constr\n";
}
A(const A& x)
{
    a = x.a;
    cout << "copy constr\n";

}

A& operator = (const A& x)
{
    a = x.a;
    cout << "equal operator\n";
    return *this;
}


void print()
{
    cout << "\n" << a << "\n";
}
};


int main()
{
     A a(3);

     A b( A(a));

     b.print();

     return 0;
}

The given error is : error: request for member 'print' in 'b', which is of non-class type 'A(A)'| I used typeid to verify what object does b become?

The name of b-s class is different from object a's class. ( a class typeid name is A1 while b's class typeid name is F1_A1LS ).

Well if i modify A b( A(a)) to A b((A(a))) it works perfect.

Can someone explain to my why this doesn't work?

Eduard6421
  • 295
  • 1
  • 11
  • 4
    [most vexing parse](https://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work)? – Cory Kramer Jun 06 '17 at 16:28
  • 1
    If you use [uniform initialization syntax](http://www.stroustrup.com/C++11FAQ.html#uniform-init), as in `A b{A{a}};`, this problem disappears. – Edward Jun 06 '17 at 16:36

0 Answers0