问题描述
我从以相同的标题开始的问题答案中看了各种各样,但无济于事,所以我不得不发布自己的版本.我将尽我所能全面解释.我收到的错误如下
I've taken a look at the wide variety on answers to questions starting with the same title as this, but to no avail so unfortunately I'm having to post my own version. I'll try explain as comprehensively as I can. The error I'm getting is as follows
错误:无效使用非静态数据成员'MainState :: pellet'
此错误与Player类文件中的功能有关,该功能正试图访问在另一个文件中声明的5个Pellet类对象的数组的成员.这是导致错误的函数:
This error relates to a function in a Player class file which is attempting to access the members of an array of 5 Pellet class objects declared in another file. Here's the function which is causing the error:
void Player::onCollide(std::list<Entity*>& entityList)
{
//create two iterators so can do self-checks
std::list<SnakePieces>::iterator i;
std::list<SnakePieces>::iterator m;
//collision with pellet
for(Entity* player : entityList)
{
Player* p = dynamic_cast<Player*>(player);
if(p)
{
i=p->Snake_List.begin();
for(int t=0; t < 4; t++)
{
if(i->x==MainState::pellet[t]->x)
{
return;
}
}
}
}
//all collisions for the first snake
auto itPlayer = entityList.begin();
Player* p = dynamic_cast<Player*>(*(itPlayer++));
Player* p2 = dynamic_cast<Player*>(*itPlayer);
if(p)
{
if(i != m)
{
i=p->Snake_List.begin();
for(m=p2->Snake_List.begin(); m != p2->Snake_List.end(); m++)
{
if ((i->x == m->x) && i->y == m->y)
{
p->respawn();
return;
}
}
}
}
该数组在MainState类头文件中声明,如下所示:
The array is declared in the MainState class header file and that looks like this:
class MainState : public prg::IAppState,
public prg::ITimerEvent
{
public:
void onRender( prg::Canvas& canvas ) override;
void onTimer(prg::Timer & Timer) override;
void checkBoundaries();
Pellet* pellet[5] {new Pellet(), new Pellet(), new Pellet(), new Pellet(), new Pellet()};
private:
//timers
prg::Timer Timer {0, 150, *this};
prg::Timer Spawn_Timer {1, 5000, *this};
//players & pellets
std::list<Entity*> players_ { new HumanPlayer( "Solid Snake"), new HumanPlayer ( "Liquid Ocelot")};
//images
prg::Image background_;
//bools
bool newPlayer = false;
bool timerRunning = false;
};
我已经删除了大量与该问题无关的代码,但是我无法理解问题的根源以及为什么我无法访问此数据.我对许多C ++还是很陌生,所以我知道我做错了什么或没有在这里实现一些东西,但是尽管阅读了好一个小时左右的类似问题,还是看不到什么,对不起.希望有人能对此有所启发,谢谢!
I've cut out a great deal of code unrelated to the problem, but I just can't get my head around what the issue is and why I can't access this data. I'm pretty new to a lot of c++ so I know I've done something wrong or haven't implemented something here, but can't see what despite reading similar questions for a good hour or so, sorry. Hopefully someone can shine some light on this, thanks!
推荐答案
如错误所示, pellet
是 MainState
的非静态成员;因此您只能将其作为 MainState
对象的一部分进行访问.您正在尝试像访问静态成员一样访问它,该成员独立于任何对象而存在.
As the error says, pellet
is a non-static member of MainState
; so you can only access it as part of a MainState
object. You are trying to access it as if it were a static member, which exists independently of any objects.
如果您的 Player
类需要访问它,则它将需要对它或包含它的 MainState
对象的引用.
If your Player
class needs to access it, then it will need a reference to it, or to the MainState
object that contains it.
这篇关于无效使用非静态数据成员C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!