[英]Invalid use of non-static Data Member c++
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
我得到的错误如下
error: invalid use of non-static data member 'MainState::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. 此错误与Player类文件中的函数有关,该文件试图访问在另一个文件中声明的5个Pellet类对象的数组的成员。 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;
}
}
}
}
The array is declared in the MainState class header file and that looks like this: 该数组在MainState类头文件中声明,如下所示:
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;
};
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.
我对很多c ++都很陌生,所以我知道我做错了什么或者没有在这里实现一些东西,但是尽管在一个小时左右阅读了类似的问题但是看不清楚,对不起。 Hopefully someone can shine some light on this, thanks!
希望有人可以对此有所启发,谢谢!
As the error says, pellet
is a non-static member of MainState
; 正如错误所说,
pellet
是MainState
的非静态成员; so you can only access it as part of a MainState
object. 因此您只能将其作为
MainState
对象的一部分进行访问。 You are trying to access it as if it were a static member, which exists independently of any objects. 您试图访问它,就好像它是一个静态成员,它独立于任何对象存在。
If your Player
class needs to access it, then it will need a reference to it, or to the MainState
object that contains it. 如果您的
Player
类需要访问它,那么它将需要对它的引用,或者需要包含它的MainState
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.