2017-10-24 5 views
1

내 질문은 다른 템플릿의 매개 변수 일 때 템플릿의 "형식"을 지정해야합니까? 이것은 전문화 된 방법입니다.클래스 템플릿 인 클래스 템플릿 메서드의 특성화 템플릿 오류 : 형식/값이 일치하지 않는 경우 인수가

당신을 문맥에 넣을 수있게하십시오.

템플릿 컴퓨터 클래스가있는 TicTacToe 게임을하고 있습니다. 그래서, 난 매개 변수에 어려움 수준을 설정할 수 있습니다. 이 그것의 샘플입니다

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*/ 
} 

그런 다음, 나는 tempated를 사용하여 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. 
    */ 
} 

내 질문에 반환. 조직

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

이러한 유형의

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() 

그리고 그 이유를 이해할 수 없다 :

그것은하는 오류를 많이 반환합니다. 도움이 필요해.

+0

처럼 사용할 수 있습니까? 왜냐하면 그렇지 않기 때문에 HumanPlayer 및 ComputerPlayer 클래스에 의해 구현 된 Player 인터페이스를 사용하는 것 이상의 템플릿을 사용하는 이점을 실제로 볼 수 없기 때문입니다 ... –

+0

시작했을 때 프로젝트 결정이었습니다. 늦게 나는 그것이 최고의 선택이 아니라는 것을 알아 차렸다. 그러나, 당신의 생각은 내 마음 속에 오지 않았고 훨씬 직접적이고 간단했습니다. 그리고 나서 제가 생각해 냈습니다. 만약 당신이 상관 없다면, 나는 당신이 말한 것을 전함 프로젝트 – Homunculus

답변

1

Computer은 클래스 템플릿입니다. 템플릿 인수를 사용하려면 템플릿 인수를 지정해야합니다. 예 :

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

또는 int 템플릿 매개 변수를 Computer 같은 클래스 템플릿 할 수 있습니다 partial specifyTicTacToe. .e.g

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

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

는 나는이 실험을 위해 단지 가정

TicTacToe<int, Computer<42>> t; 

LIVE

+0

을 구축하기 위해 사용하게 될 것입니다. 그렇기 때문에 첫 번째 제안은 20 가지 유형의 난이도가 있다면 20 가지 유형의 전문화를하도록 강요합니다. TicTacToe 클래스에서 동일합니까? 무효 포인터와 같은 옵션이 없나요? – Homunculus

+1

@Homunculus Answer Revised. – songyuanyao