我正在试图理解C++的语法,因为我对这门语言还很陌生,但我不知道我到底面临着什么错误。
我在代码上实现了组件类,并且运行良好。
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++ 指南书籍,但没有在那里找到答案,因为我可能不知道如何引用这个问题。提前感谢
发布于 2013-09-10 21:00:49
在class
声明中定义方法时,不能使用 scope resolution operator。
而且,您的方法可能应该公开。最后,您必须确保您的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
};
发布于 2013-09-10 21:05:30
在这段代码中,您使用的是mChildren,但容器类中没有定义它。mChildren应该是什么?
如果它是Component::Ptr
的向量,则需要在类中定义它。
std::vector<Component::Ptr>mChildren;
发布于 2013-09-10 21:24:11
为什么要在构造函数初始化列表中初始化mChildren
?更确切地说,这个叫做mChildren()
在做什么?试着删除那个调用,看看会发生什么。
https://stackoverflow.com/questions/18728634
复制相似问题