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

最后都变了- 提交于 2020-01-30 05:57:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!