Vic*_*ira 2 c++ namespaces class
我正在尝试理解C++的语法,因为我对语言几乎是新鲜的,但我不知道我正面临着什么样的错误..
我在我的代码上实现了Component类,并且运行正常
namespace GUI
{
class Component : public sf::Drawable
, public sf::Transformable
, private sf::NonCopyable
{
public:
//Variables
};
}
Run Code Online (Sandbox Code Playgroud)
还有我学习的书要求我在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;
}
Run Code Online (Sandbox Code Playgroud)
我没有得到的是他实现类的方式,这给了我帖子标题中的语法错误."错误:"mChildren"不是非静态数据成员或类的基类"GUI ::容器"".
我尝试了更进一步的代码:
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;
}
};
Run Code Online (Sandbox Code Playgroud)
但是我仍然会遇到语法错误= /究竟出了什么问题以及我对此主题的看法是什么?(我也阅读了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
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5876 次 |
| 最近记录: |