3

I have the following three class definitions:

class String
{
    public:
        String() {}
        String(const char *) {}
};

class ClassA
{
    public:
        ClassA(const String &) {}
};

class ClassB
{
    public:
        ClassB(const ClassA &, const String & = String()) {}
        void method() {}
};

Now suppose I want to create an instance of ClassB:

String name("test");
ClassA item(ClassB(name));

This doesn't work:

error: request for member 'method' in 'item', which is of non-class
  type 'ClassA ()(ClassB)'

What does this error mean? And what is this strange type ClassA ()(ClassB) the compiler keeps referring to?

LihO
  • 41,190
  • 11
  • 99
  • 167
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
  • 1
    Are you sure that's exactly how `item` is declared? – aschepler Oct 08 '13 at 00:00
  • 1
    `ClassA item(ClassB(name));` seems incorrect as `ClassA` doesn't take a type `ClassB` in the constructor, it only takes `String`. `ClassB` doesn't appear to have any valid conversions to `String`. Perhaps this was a typo and you meant `ClassB item(ClassA(name));`? – gigaplex Oct 08 '13 at 00:04

1 Answers1

8

This is called most vexing parse problem.

ClassA item(ClassB(name));

should either be:

ClassB b(name);
ClassA item(b);

or:

ClassA item( (ClassB(name)) );

Also have a look at: Most vexing parse: why doesn't A a(()); work?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167