无法使用向量作为键创建地图,而将自定义类作为值创建

时间:2010-10-17 22:23:29

标签: c++ vector maps

我在下面创建了抽象类来评估简单游戏的董事会职位。每个派生类都会覆盖抽象类,以便只在game.h中定义evaluate函数

我试图通过使用memoization使我的程序更有效率,但我无法让我的地图正常工作。编译器会为行结果[board] = best抛出错误。该行试图将映射到当前板(int的向量)的值设置为从该位置移动到最佳位置。移动是我创建的一个类,它只包含一个分数,一个要删除以制作下一个板的数字,以及用于删除该数字的索引(桩)。

'results [board] = best'的编译错误表示没有匹配函数调用move :: move()。我不明白这个错误,因为我不想创建一个新的举动,只是存储当前最好的举动。我试过创建一个临时移动并存储它,所以我知道这是不正确的。

最后一点 - 代码编译并在没有该代码行的情况下完美运行,因此我知道算法和所有子类都能正常工作。任何帮助,将不胜感激!

// VIRTUAL FUNCS
// All virtual functions must be overridden by subclasses

virtual ~game(){ }

// initialize
virtual void initialize( int numPlayers, std::vector<int> board ) = 0;


// gameHasEnded
virtual bool gameHasEnded( std::vector<int> board ) const = 0;

// display
virtual void display( std::vector<int> board ) const = 0;

// makeNewBoard
virtual std::vector<int> makeNewBoard( std::vector<int> currBoard, move m ) = 0;

// generateMoves
virtual std::vector< move >  generateMoves( std::vector<int> currBoard ) = 0;

// compare
virtual move compare( std::vector<int> board1, std::vector<int> board2 ) = 0;

// NON-VIRTUAL FUNCS

//
// Name:         evaluate
// Description:  Evaluates a given board position. Determines whether
//                 or not the current position is a winning position
//                 or a losing position. A winning position is
//                 designated by a 1, a losing by a -1.
// Modifies:     The board and the score.
// Returns:      The best possible move from this position.
//                
move evaluate(  std::vector<int> board, int multiplier = 1, int currScore = -100) {

  // Base case is defined by subclasses
  if( gameHasEnded( board ) ) {
    move toReturn(multiplier, 0);
    return toReturn;
  } // end-if

  // If game is not over
  else {

    std::map<std::vector<int>,move>::iterator iter = results.find( board );
    if( iter != results.end() ) {
      return iter->second;
    }
    else {

      move best(-2,0);  // Just a place holder. Is overridden later.
      int s = 0;  // Stores the score

      std::vector<move> nextMove;
      // generate all possible boards from this position - defined by subclass
      nextMove = generateMoves( board );

      // For each board position
      for( int i = 0; i < ( (int)nextMove.size() ); ++i ) {
        std::vector<int> newBoard;

        // Create a new possible board state
        newBoard =  makeNewBoard( board, nextMove[i] );

        move dif = compare( board, newBoard );  // Get the move that was made
        move m(0,0);  // place holder
        m = evaluate( newBoard, multiplier*-1, currScore );  // recurse through all positions
        s += m.getScore();
        // If this is the best score so far
        if( m.getScore() > currScore ) {  

          m.setNumTake( dif.getNumTake() );  // get the move
          m.setPile( dif.getPile() );
          best = m;  // store the move
          currScore = m.getScore();  // update the score

        }

      }
      best.setScore( s );

      ////////////////////////////// THIS IS THE LINE THAT THROWS A COMPILER ERROR

      results[ board ] = best;

      //////////////////////////////////

      return best;  // return best move
    }
  }
    return move(2,2);  // dummy return. should never happen
  }

private://数据成员

std::map<std::vector<int>,move> results;

};

2 个答案:

答案 0 :(得分:3)

必须能够使用默认构造函数创建在[]运算符的地图中用作值的任何类。

results[ board ] = best;

将执行以下操作

  1. 使用密钥move()
  2. 创建默认 board
  3. 返回对该地址的引用
  4. 通过为其分配best来覆盖默认移动。
  5. 你正在失败第1步。

答案 1 :(得分:2)

如果使用operator [],则需要默认构造函数。

如果地图不包含键board的元素,则map的operator []构造一个新的默认对象并将其插入到地图中。只有这样才能完成best

的分配
相关问题