我正在尝试将模板化方法添加到模板化类中。我提到了 this回答但是语法不起作用。我添加了第二种方法,称为 tester
我想对其进行模板化。这是我的
template <typename t,typename u>
struct foo {
void test();
template<typename v>
void tester(v lhs);
};
template<typename t,typename u>
void foo<t,u>::test() {
std::cout << "Hello World" ;
}
template<typename t,typename u>
template<typename v>
void foo<t,u>::tester<v>(v lhs) {
std::cout << "Hello world " << lhs ;
}
int main()
{
foo<int,int> t;
t.test();
t.tester<int>(12);
}
我收到方法 tester
的错误,这是我收到的错误
main.cpp:20:31: error: non-type partial specialization 'tester<v>' is not allowed
void foo<t,u>::tester<v>(v lhs) {
关于为什么我会收到此错误或我可能做错了什么有什么建议吗?
最佳答案
在下面更正的代码中内嵌注释:
#include <iostream>
template <typename t,typename u>
struct foo {
void test();
template<typename v>
void tester(v lhs);
};
template<typename t,typename u>
void foo<t,u>::test() {
std::cout << "Hello World" ;
}
/*
* change made to this template function definition
*/
template<typename t,typename u>
template<typename v>
void foo<t,u>::tester(v lhs) {
std::cout << "Hello world " << lhs ;
}
int main()
{
foo<int,int> t;
t.test();
t.tester<int>(12);
}
关于c++ - 在模板化类中声明模板化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45687016/