簡體   English   中英

類模板方法的特化,類型名是類模板 - 錯誤:參數的類型/值不匹配

[英]Specialization of a class template method, with typenames that are class template - error: type/value mismatch at argument

我的問題是,當它是另一個模板的參數時,我是否必須指定模板的“類型”? 這是一種方法的專業化。

讓我允許把你放在上下文中。

我正在做一個井字游戲,其中有一個模板計算機類。 所以,我可以在參數中設置它的難度級別。 這是它的一個示例:

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

我不明白為什么。 我需要你的幫助。

Computer是一個類模板,使用時必須指定模板參數,例如

template<>
int TicTacToe<Human,  Computer<42>>::newTurn()

或者,您可以為類模板部分指定TicTacToe ,例如采用int模板參數的Computer 。例如

template <typename T1, template <int> class T2, int I>
class TicTacToe<T1, T2<I>>

{
    T1 &Player1;        
    T2<I> &Player2;
    ...
};

然后像這樣使用它

TicTacToe<int, Computer<42>> t;

居住

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM