类模板方法的专用化,类型名称是类模板 - 错误:参数处的类型/值不匹配
Specialization of a class template method, with typenames that are class template - error: type/value mismatch at argument
我的问题是,当它是另一个模板的参数时,我是否必须指定模板的"类型"?这是一种方法的专业化。
让我允许把你放在上下文中。
我正在做一个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*/
}
然后,我想出了一个想法,创建一个试探的 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()
或者,您可以为类模板(如 Computer
)部分指定TicTacToe
,它采用int
模板参数。
template <typename T1, template <int> class T2, int I>
class TicTacToe<T1, T2<I>>
{
T1 &Player1;
T2<I> &Player2;
...
};
然后像
TicTacToe<int, Computer<42>> t;
住
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- C++ 模板:重载时找不到基类类型参数方法