[英]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.