来自班级的访问列表,在父级中
我是 C++ 新手,并且有使用其他编程语言的经验,但我有一个问题: 如何从子类、父类访问列表?
这是我的布局: 带有函数TutorialApp::update() 的TutorialApp.cpp 在该函数中,我想从另一个类访问列表 mParticles2y 该列表是在:ParticleController.cpp 中创建的,如下所示:
std::list<int> mParticles2y;
我尝试像这样访问该列表[在TutorialApp 中]: mParticleController.mParticles2y.size()
[获取它的大小]
但这给了我这个错误:
在没有适当运算符的情况下调用类类型的对象
所以我真的不知道从这里去哪里...
PS:我使用 mParticleController 因为这是我的脚本中的状态:
ParticleController mParticleController;
我希望这是足够的信息。
PS:我不太确定这称为类或子类,我使用这些术语是因为我从 ActionScript 中了解它们[它以类似的方式与类一起工作]
I'm new to C++, and have experience with other programmas languages, but I have a question:
How can I access a list from a sub class, from the parent?
Here is my layout:
TutorialApp.cpp with function TutorialApp::update()
In that function I want to access the list mParticles2y from another class
that list is made in: ParticleController.cpp, like this:
std::list<int> mParticles2y;
I've tried accessing the list like this [in TutorialApp]:mParticleController.mParticles2y.size()
[to get it's size]
but that gives me this error:
call of an object of a class type without appropriate operator
So I dont really know where to go from here...
PS: I use mParticleController because that is state in my script:
ParticleController mParticleController;
I hope this is enough info.
PS: I'm not really sure this is called a class, or child, I use these terms because I know them from ActionScript [which works with classes in a similar way]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有错误的循环代码应该看起来更像这样。它可能无法准确编译,因为我目前无法轻松编译它。这不是解决问题的理想方法,但它对代码的影响最小。我会将此循环作为成员函数移至 ParticleController 类,该成员函数返回 true/false 以指示命中。这样封装起来会更好。
}
Your code for the loop that has the errors should look more like this. It may not compile exactly since I can't compile it easily at the moment. This isn't the ideal way to go about it, but it is the minimal impact to your code. I would move this loop to the ParticleController class as a member function that returned true/false to indicate a hit. It would be better encapsulated that way.
}
好的,所以这是一个盲目的尝试,因为你的问题虽然非常冗长,但缺乏重现该问题所需的代码。
您只能访问其他对象的公共成员(数据或函数)。也就是说,为了访问
mParticleController.mParticles2y
,mParticles2y
必须是任何类型的公共成员mParticleController
是。当然,公共数据成员不受欢迎,这是有充分理由的。一个类应该代表一个抽象,如果你有一个粒子控制器,它应该实现控制粒子所需的一切,而不是把它的内容公之于众,让每个人筛选并获取他们需要的东西。
这称为封装,是面向对象范式的基石之一。
Ok, so this is a shot in the dark, since your question, while very wordy, is short on code necessary to reproduce the issue.
You can only access public members (data or functions) of other objects. That is, in order to access
mParticleController.mParticles2y
,mParticles2y
must be a public member of whatever typemParticleController
is of.Of course, public data member are frowned upon, and for good reasons. A class should represent an abstraction, and if you have a particle controller, it should implement everything necessary to control particles, rather than spilling its guts out in the public for everyone to sift through and take what they need.
This is called encapsulation, and one of the cornerstones of the object oriented paradigm.
size
是一个方法,需要写size()
。size
is a method, you need to writesize()
.您提供的信息有些含糊,但看起来您可能正在尝试从另一个类(TutorialApp)访问一个类(ParticleController)的私有状态(列表)。
我假设以下代码结构(请注意,我没有尝试编译它,所以它可能不太正确):
The information you provide is somewhat ambiguous, but it looks like you may be attempting to access the private state (the list) of one class (the ParticleController) from another class (The TutorialApp).
I'm assuming the following code structure (note that I haven't tried to compile this so it might not be quite right):