Search code examples
c++most-vexing-parse

Invalid object after elision of copy operation?


Possible Duplicate:
Why is it an error to use an empty set of brackets to call a constructor with no arguments?
Most vexing parse: why doesn't A a(()); work?

This one gets me mad. Maybe its just too simple.

struct Foo
{
  Foo() {}
  Foo(const Foo& f) {}
  void work() {}
};

int main()
{
  Foo f( Foo() );
  f.work();
}

GCC 4.6 gives me:

error: request for member ‘work’ in ‘f’, which is of non-class type ‘Foo(Foo (*)())’

After elision of the copy operation the effective code might look like:

int main()
{
  Foo f;
  f.work();
}

But why can't i call work() ??

Edit:

Yes, duplicate (see below). Didn't find the original post when search first because the source of the symptoms of this is located where i didn't expect that.


Solution

  • Because Foo f( Foo() ); is a function declaration.

    I think you want: Foo f;

    Or in case you want to copy-construct:

    Foo f( (Foo()) );