简体   繁体   English

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

[英]Error: “Function” is not a Non static data member or base class of “Class”

I'm trying to understand the syntax of C++ since I'm almost fresh to the language, but I dont know what error exactly I'm facing.. 我正在尝试了解C ++的语法,因为我几乎是该语言的新手,但是我不知道我到底要面对什么错误。

I have the Component class implemented on my code and works fine 我在代码上实现了Component类,并且工作正常

namespace GUI
{
class Component : public sf::Drawable
                , public sf::Transformable
                , private sf::NonCopyable
{
    public:
            //Variables
};
}

and also the book Im studying asks me to implement another class called Container in the GUI namespace 我正在学习的书也要求我在GUI名称空间中实现另一个名为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;
}

What I don't get is the way he is implementing the class, which is giving me the syntax error in the title of the post.. "Error: "mChildren" is not a Non static data member or base class of class "GUI::Container"" . 我没有得到的是他实现类的方式,这给了我帖子标题中的语法错误。 “错误:“ mChildren”不是类“ GUI”的非静态数据成员或基类:: Container“”

I tryed the futher code: 我尝试了进一步的代码:

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;
}
};

But I'm still getting syntax error =/ what exactly is going wrong and what shoul I read regarding this subject? 但是我仍然收到语法错误= //到底出了什么问题以及关于该主题我应该读些什么? (I also read C++ guidelines books but I didnt find the answer there cos I probably dont know how to refer to this problem) thanks in advance (我也读过C ++指南书,但是我在那里找不到答案,因为我可能不知道该如何解决这个问题)

When you define your methods inside your class declaration, you can't use the :: scope resolution operator . class声明中定义方法时, 不能使用 ::作用域解析运算符

Also your methods should probably in public. 同样,您的方法可能应该公开。 And finally you have to be sure that your mChildren member is correctly define. 最后,您必须确保您的mChildren成员正确定义。

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

};

From this code you are using mChildren, but it is not defined in your Container class. 通过此代码,您正在使用mChildren,但未在Container类中定义它。 What is mChildren supposed to be? mChildren应该是什么?

If it is a vector of Component::Ptr you need to define it in your class. 如果它是Component::Ptr的向量,则需要在您的类中定义它。

std::vector<Component::Ptr>mChildren;

Why are you initializing mChildren in the constructor initialization list? 为什么要在构造函数初始化列表中初始化mChildren More specifically what is this call mChildren() doing? 更具体地说,此调用mChildren()什么? Try removing that call and see what happens. 尝试删除该呼叫,然后看看会发生什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类” - Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name 枚举不是类的非静态数据成员或基类 - enum is not a non-static data member or base class of class C ++派生类是否可以从Base类继承静态数据成员和静态成员函数? - C++ Does derived class could inheritance Static data member and Static Member function from Base class? 基类成员函数内部的静态变量 - static variable inside member function of base class 类到模板错误:无效使用非静态数据成员 - Class to template error: invalid use of non-static data member 成员初始化程序“ SuperClass”未命名非静态数据成员或基类 - member initializer 'SuperClass' does not name a non-static data member or base class 该类中已删除的析构函数显示为虚拟/直接基类或非静态数据成员的类型 - Deleted destructor in the class appeared as a virtual/direct base class or as a type of non-static data member 在类外部访问非静态数据成员 - Access non static data member outside class 将非静态成员 function 传递给另一个 class 的成员 function - Pass non-static member function to member function of another class 模板类和继承问题-“列表”未命名非静态数据成员或基类 - Issues with template classes and inheritance - 'List' does not name a non-static data member or base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM