使用回调函数就出错,直接调用没问题

问题已解决,刚开始在HelloWorld的init函数里边只是创建了三个player,player1 player2 player3,但是没有执行addchild
后来分别把三个player执行addchild问题就解决了。
虽然还不知道是什么原因,但是好歹是解决了。

最近在写一个斗地主游戏,写到发牌的时候遇到了问题。
发牌所有操作写在了一个deal函数里,直接调用deal的话不会出错,但是如果用回调函数就会出错。
希望大家帮忙看一下是什么问题,谢谢了。

void HelloWorld::deal(Ref* r)
{
    int mark = 1; //用来标记给哪个玩家发牌
    std::vector<Card*>::iterator it = allCardList.begin(); //allCardList是std::vector<Card*>类型的,存放着所有54张牌
    for (int i = 0; i < 51; i++)
    {
        auto tmp = *it;
        switch (mark)
        {
        case 1:
            tmp->showFront();
                        //getCardsCounts()函数返回当前玩家有几张牌
            tmp->runAction(MoveTo::create(0.02f * (i + 1), Vec2(125 + player1->getCardsCount() * (CARD_WIDTH - 15.625), 70)));
            tmp->setLocalZOrder(i + 10);
                        //getCardList()函数返回玩家类内存放牌的vector
            player1->getCardList().push_back(tmp); //使用回调函数时这里出错,应该是push_back操作有问题。但是直接调用这个函数就不会出错
            player1->setCardsCount(player1->getCardsCount() + 1);
            mark++;
            break;
        case 2:
            tmp->showBack();
            tmp->runAction(MoveTo::create(0.02f * (i + 1), Vec2(50, 480 - 100 - player2->getCardsCount() * (CARD_HEIGHT - 55))));
            tmp->setLocalZOrder(i + 10);
            player2->getCardList().push_back(tmp);
            player2->setCardsCount(player2->getCardsCount() + 1);
            mark++;
            break;
        case 3:
            tmp->showBack();
            tmp->runAction(MoveTo::create(0.02f * (i + 1), Vec2(800 - 50, 480 - 100 - player3->getCardsCount() * (CARD_HEIGHT - 55))));
            player3->getCardList().push_back(tmp);
            tmp->setLocalZOrder(i + 10);
            player3->setCardsCount(player3->getCardsCount() + 1);
            mark = 1;
            break;
        }
        it++;
    }
}

运行中断时执行的代码

    void _Orphan_range(pointer _First, pointer _Last) const
        {    // orphan iterators within specified (inclusive) range
        _Lockit _Lock(_LOCK_DEBUG);
        const_iterator **_Pnext = (const_iterator **)this->_Getpfirst();
        if (_Pnext != 0) //查看_Pnext的内存不可访问,全是问号
            {    // test an iterator
            while (*_Pnext != 0)
                if ((*_Pnext)->_Ptr < _First || _Last < (*_Pnext)->_Ptr)
                    _Pnext = (const_iterator **)(*_Pnext)->_Getpnext();
                else
                    {    // orphan the iterator
                    (*_Pnext)->_Clrcont();
                    *_Pnext = *(const_iterator **)(*_Pnext)->_Getpnext();
                    }
            }
        }

调用方法
auto dealLable = MenuItemFont::create(“Deal”, CC_CALLBACK_1(HelloWorld::deal, this));
dealLable->setPosition(Vec2(visibleSize.width / 2, visibleSize.height - 50));

迭代器失效

不太明白,可以说详细一点吗

循环体内对被循环容器有修改,这是大禁忌

修改的不是被循环的容器,是把被循环的容器中的元素push_back到另外一个容器内。
直接用函数名调用这个函数就没问题,可以达到预期的效果。但是添加到按钮的回调函数就不行