0
class G
{
    public:
    G() { puts("G"); }
    G(const G& g) { puts("copy"); }
}
int main()
{
    G A();
    G B(A);
}

Compiling the code above with g++ -std=c++11 will cause error: no matching function for call to ‘G::G(G (&)())’.

But if I replace the code G A(); with G A;,everything is ok.

Can anyone tell me why ? And what's the difference between G A(); and G A;

cigien
  • 57,834
  • 11
  • 73
  • 112
max
  • 29
  • 3
  • 4
    `G A();` declares a *function* which takes no argument and returns a `G` object by value. Either drop the parentheses or use curly-braces. – Some programmer dude Jul 07 '21 at 12:43
  • 3
    Just another victim of [the most vexing parse](https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse). – Bob__ Jul 07 '21 at 12:57

0 Answers0