0

The code is as below

class A {};
class B
{
public:
    B(const A& a) {}
    void fun() {}
};

int main(int argc, char *argv[])
{
    B b(A());
    b.fun(); // Error: left of '.fun' must have class/struct/union  

    A a;
    B b2(a);
    b2.fun(); //Okay

    return 0;
}

Why?

user1899020
  • 13,167
  • 21
  • 79
  • 154

1 Answers1

2

The code

 B b(A());

is not declaring an object of B, but rather a function declaration for a function b which returns an object of type B and takes a single (unnamed) argument which is a function returning type A(and taking no input). (Quoting from link below). Therefore, you saw that error.

See C++ most vexing parse

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
taocp
  • 23,276
  • 10
  • 49
  • 62