我的问题是,当它是另一个模板的参数时,我是否必须指定模板的“类型”?这是一种方法的专业化。
让我允许把你放在上下文中。
我正在做一个井字游戏,其中有一个模板计算机类。所以,我可以在参数中设置它的难度级别。这是它的一个示例:
template<int T>
class Computer
{
Node *Root; /**< Root of a decision tree */
Node *Current; /**< Current node in a decision tree*/
int PlayerNumber; /**< Player ID*/
int OponnentNumber /**< Opponent ID*/
Public:
/**< Constructor destructor */
int refreshBoard(int move);
int play()const; /**< This methods has different implementations, for each level of difficulty*/
}
然后,我想出了一个想法,创建一个 TicTacToe 类,以便参数接收不同类型的玩家。这是一个样本。
template <typename T1, typename T2>
class TicTacToe
{
T1 &Player1; /**< Simulates the first player */
T2 &Player2; /**< Simulates the second player */
Board &b__; /**< Simulates a board */
int TurnCounter;
public:
int newTurn();
/* This method is implemented differently for each type of combination of player
* Lets say player 1 is Human and player 2 is computer. The new turn will
* change the state of the board and will return 1 if there is still new turns
* to come.
*/
}
回到我的问题:我在设置正确的语法时遇到问题,所以编译器理解我。
它返回很多错误:
error: type/value mismatch at argument 2 in template parameter list for ‘template<class T1, class T2> class TicTacToe’
int JogoVelha<Human,Computer>::newTurn()`
note: expected a type, got ‘Computer’
header/TicTacToe.h:201:40: error: ‘newTurn’ is not a template function
int TicTacToe<Human,Computer>::newTurn()
对于此类组织
template<>
int TicTacToe<Human,Computer>::newTurn()
...implementation
我不明白为什么。我需要你的帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句