Why can't I access a member of this class? [du

2019-02-24 20:18发布

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条回答
仙女界的扛把子
2楼-- · 2019-02-24 20:25

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?

查看更多
登录 后发表回答