列表中的指针问题
由于某种原因,当我尝试读取 std::list
(playerlist
) 内的对象 (GamePlayer
) 指针的属性时,它起初工作,但是当我稍后尝试在另一个函数中访问它时,我得到一堆随机数,而不是客户端套接字的数字。那是一口,抱歉。我希望有人能够阐明这一情况。我将包含有缺陷代码的简化版本。
class GameRoom {
list<GamePlayer*> playerlist;
locigPort( LogicObj );
}
bool GameRoom::logicPort( LogicObj logit ) { // This is room[1]
list<GamePlayer*>::iterator it;
for (it = playerlist.begin(); it != playerlist.end(); it++){
cout << "socket numbers " << (*it)->socketno << endl;
/* (*it)->socketno gives me a bunch of random numbers,
not the socket numbers I was looking for! */
}
return true;
}
bool RoomDB::addPlayer( GamePlayer *playerpoint ) {
roomlist[1].playerlist.push_back( playerpoint );
// This adds the player object to the Gameroom object
cout << "player point " << playerpoint->socketno << " roomno: " << roomno;
// This shows everything should be ok so far
return true;
}
For some reason when I try and read a property of a pointer to an object(GamePlayer
) within an std::list
(playerlist
) it works at first, but when I try to access it later in another function I get a bunch of random numbers instead of the numbers for my client's socket. That was a mouthful, sorry. I hope someone could shed some light on the situation. I will include a simplified version of the defective code.
class GameRoom {
list<GamePlayer*> playerlist;
locigPort( LogicObj );
}
bool GameRoom::logicPort( LogicObj logit ) { // This is room[1]
list<GamePlayer*>::iterator it;
for (it = playerlist.begin(); it != playerlist.end(); it++){
cout << "socket numbers " << (*it)->socketno << endl;
/* (*it)->socketno gives me a bunch of random numbers,
not the socket numbers I was looking for! */
}
return true;
}
bool RoomDB::addPlayer( GamePlayer *playerpoint ) {
roomlist[1].playerlist.push_back( playerpoint );
// This adds the player object to the Gameroom object
cout << "player point " << playerpoint->socketno << " roomno: " << roomno;
// This shows everything should be ok so far
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最可能的解释是,您调用
addPlayer
时使用的指针在调用logicPort
时变得无效。一种可能性是您使用堆栈上对象的地址调用addPlayer
,并且该对象在堆栈展开时消失。编辑 问题就在这里:
PlayerDB::addPlayer
按值获取第二个参数。这意味着它会获得一个在该方法的生命周期内存在的副本。然后,您将指针指向该副本,并将其添加到列表中。一旦PlayerDB::addPlayer
返回,指针就变得无效。如果没有看到更多代码,很难提出好的修复建议。一种可能性是让
PlayerDB::addPlayer
将指针作为其第二个参数,并确保您不会在调用链的上一级重复相同的错误。更好的可能性是将
playerlist
转换为list
:从您的代码来看,列表似乎不需要包含指针 /em>.这将大大简化事情。The most likely explanation is that you're calling
addPlayer
with a pointer than becomes invalid by the time you calllogicPort
. One possibility is that you calladdPlayer
with the address of an object on the stack, and the object disappears when the stack is unwound.edit The problem is right here:
PlayerDB::addPlayer
takes the second argument by value. This means that it gets a copy that exists for the lifetime of the method. You then take the pointer to that copy, and add it to the list. OncePlayerDB::addPlayer
returns, the pointer becomes invalid.It's hard to suggest a good fix without seeing more code. One possibility is to make
PlayerDB::addPlayer
take a pointer as its second argument, and make sure you don't repeat the same mistake one level up the call chain.An even better possibility is to turn
playerlist
intolist<GamePlayer>
: from your code there doesn't appear to be any need for the list to contain pointers. This will simplify things greatly.