[英]Trouble creating a map with vector as key and custom class as value
我在下面創建了抽象類來評估簡單游戲的董事會職位。 每個派生類都會覆蓋抽象類,因此在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;
};
使用[]運算符在地圖中用作值的任何類都必須能夠使用默認構造函數創建。
results[ board ] = best;
將做以下事情
move()
與鍵board
best
來覆蓋默認移動。 你失敗了第1步。
如果使用operator [],則需要默認構造函數。
如果地圖不包含用於所述鍵的元素board
,操作符[]映射的構造一個新的默認對象,並將其插入到地圖中。 只有這樣才能做出best
的分配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.