首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:“函数”不是非静态数据成员或“类”的基类。

错误:“函数”不是非静态数据成员或“类”的基类。
EN

Stack Overflow用户
提问于 2013-09-10 20:57:30
回答 3查看 9.6K关注 0票数 2

我正在试图理解C++的语法,因为我对这门语言还很陌生,但我不知道我到底面临着什么错误。

我在代码上实现了组件类,并且运行良好。

代码语言:javascript
运行
复制
namespace GUI
{
class Component : public sf::Drawable
                , public sf::Transformable
                , private sf::NonCopyable
{
    public:
            //Variables
};
}

此外,我正在学习的书还要求我在GUI命名空间中实现另一个名为Container的类

代码语言:javascript
运行
复制
Container::Container()
: mChildren()
, mSelectedChild(-1)
{
}
void Container::pack(Component::Ptr component)
{
    mChildren.push_back(component);
    if (!hasSelection() && component->isSelectable())
        select(mChildren.size() - 1);
}
bool Container::isSelectable() const
{
    return false;
}

我不明白的是他实现类的方式,这导致帖子标题出现语法错误:

代码语言:js
复制
Error: mChildren is not a Nonstatic data member or a base class of class GUI::Container.

我尝试了进一步的代码:

代码语言:javascript
运行
复制
class Container:

{

Container::Container()
: mChildren()
, mSelectedChild(-1)
{
}
void Container::pack(Component::Ptr component)
{
    mChildren.push_back(component);
    if (!hasSelection() && component->isSelectable())
        select(mChildren.size() - 1);
}
bool Container::isSelectable() const
{
    return false;
}
};

但我仍然遇到语法错误。到底是什么问题?我应该阅读哪些相关资料?

注意:我也阅读了 C++ 指南书籍,但没有在那里找到答案,因为我可能不知道如何引用这个问题。提前感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-10 21:00:49

class声明中定义方法时,不能使用 scope resolution operator

而且,您的方法可能应该公开。最后,您必须确保您的mChildren成员定义正确。

代码语言:javascript
运行
复制
class Container
{
    // ...

public:
    Container()
//  ^^
       : mChildren()
       , mSelectedChild(-1)
    {
    }
    void pack(Component::Ptr component)
//       ^^
    {
         // ...
    }
    bool isSelectable() const
//       ^^
    {
        // ...
    }

private:
    std::vector<Component::Ptr> mChildren;   // Example of a definition of mChildren
    //          ^^^^^^^^^^^^^^ replace with the good type

};
票数 7
EN

Stack Overflow用户

发布于 2013-09-10 21:05:30

在这段代码中,您使用的是mChildren,但容器类中没有定义它。mChildren应该是什么?

如果它是Component::Ptr的向量,则需要在类中定义它。

代码语言:javascript
运行
复制
std::vector<Component::Ptr>mChildren;
票数 0
EN

Stack Overflow用户

发布于 2013-09-10 21:24:11

为什么要在构造函数初始化列表中初始化mChildren?更确切地说,这个叫做mChildren()在做什么?试着删除那个调用,看看会发生什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18728634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档