错误:"Function"不是非静态数据成员或基类 "Class"
Error: "Function" is not a Non static data member or base class of "Class"
我在代码上实现了Component
类并且工作正常
namespace GUI {
class Component: public sf::Drawable, public sf::Transformable, private sf::NonCopyable {
public:
//Variables
};
}
还有我正在学习的书要求我在 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;
}
我不明白的是他实现类的方式,这给了我帖子标题中的语法错误:
Error: mChildren is not a Nonstatic data member or a base class of class GUI::Container.
我尝试了进一步的代码:
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++指南书籍,但我没有找到答案,因为我可能不知道如何参考这个问题。
在 class
声明中定义方法时,不能使用 ::
范围解析运算符。
此外,您的方法可能应该在公共场合使用。最后,您必须确保正确定义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
};
从此代码中,您使用的是 mChildren,但它未在容器类中定义。儿童应该是什么?
如果它是Component::Ptr
的向量,则需要在类中定义它。
std::vector<Component::Ptr>mChildren;
为什么要在构造函数初始化列表中初始化mChildren
?更具体地说,这个电话mChildren()
做什么?尝试删除该调用,看看会发生什么。
相关文章:
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 枚举不是非静态数据成员或类的基类
- 通过使用具有私有数据成员的类重载 + 运算符来添加分数
- 是否有必要重载具有另一个类 B 的数据成员的类 A 的赋值运算符和复制构造函数?
- 指向成员的 C++ 指针(指向成员基类的指针)
- 如何创建一个将队列作为数据成员的类,该成员在 c++ 中存储另一个类的实例
- 没有任何数据成员的类会导致内存泄漏吗?
- 使用 #include 按<algorithm>字符串数据成员对类对象的向量进行排序
- 从私有结构数据成员访问类公共成员函数C++
- 向下转换为没有数据成员"foreign"类
- 为具有一个数字数据成员的类定义所有比较运算符的方便方法
- 错误:非整型 'const char[]' 的静态数据成员的类内初始化无效
- 运算符重载与 C++ : 具有指针数据成员的类
- 作为另一个类的数据成员的类
- 如何将具有相同类型的不同矢量数据成员的类对象传递给可以对这些数据成员中的任何一个进行操作的函数
- 当我们对没有任何数据成员的类的对象使用sizeof运算符时.那么输出会是什么
- 如何在c++中处理具有不同类型的多个数据成员的类
- 复制具有shared_ptr数据成员的类的构造函数
- 用于具有const数据成员的类的move和右值赋值操作符
- 英特尔C++错误:"pair"不是非静态数据成员或类"std::pair<const int, double>"的基类