问题
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?
回答1:
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?
来源:https://stackoverflow.com/questions/19236789/why-cant-i-access-a-member-of-this-class