列表中的指针问题

发布于 2024-12-10 15:00:41 字数 1106 浏览 1 评论 0原文

由于某种原因,当我尝试读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-12-17 15:00:42

最可能的解释是,您调用 addPlayer 时使用的指针在调用 logicPort 时变得无效。一种可能性是您使用堆栈上对象的地址调用 addPlayer,并且该对象在堆栈展开时消失。

编辑 问题就在这里:

bool PlayerDB::addPlayer( int sid, GamePlayer tempplayer ) {
   ...
   roomman.addPlayer( &tempplayer, tempplayer.roomno );
}

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 call logicPort. One possibility is that you call addPlayer with the address of an object on the stack, and the object disappears when the stack is unwound.

edit The problem is right here:

bool PlayerDB::addPlayer( int sid, GamePlayer tempplayer ) {
   ...
   roomman.addPlayer( &tempplayer, tempplayer.roomno );
}

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. Once PlayerDB::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 into list<GamePlayer>: from your code there doesn't appear to be any need for the list to contain pointers. This will simplify things greatly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文