0

好的,这是我的问题:

  • 我已经实现了一个Move class, 变量Move *subMove指向一个“subMove”
  • 我有一个列表(我正在循环使用vector的s )Movevector<Move>::iterator
  • 我有一个全局 Move 对象(假设我们的链表的“根”),我们的函数最初是用它调用的
  • 我正在尝试将当前移动(从循环)分配给我当前根的subMove 字段

这就是问题出现的方式......

我的代码(大致)如下所示:

int Game::doSth(Move & pv)
{
    vector<Move> moves;
    this->possibleMoves(moves);
    for (std::vector<Move>::iterator move = moves.begin(); move!=moves.end(); ++move)
    {
        // Do sth
        int subResult = this->doSth(*move);
        if (someConditionIsTrue)
        {
            // yep, we want to store THIS move as a submove

            // This works - without memory errors - 
            // but still something goes weird, so I'd prefer creating a new object
            pv.subMove = &(*move);

            // Also tried something along the lines of `pv.subMove = new Move(*move)`
            // - yes there is an appropriate copy constructor - 
            // but this leads to a `segmentation fault` error
        }
    }

    // return something;
}

有任何想法吗?

4

0 回答 0