C++ 在模板化类中声明模板化方法

C++ 在模板化类中声明模板化方法,c++,templates,C++,Templates,我正在尝试向模板化类添加模板化方法。我提到了答案,但是语法不起作用。我添加了第二个方法,名为tester,我希望将其模板化。这就是我所拥有的 template <typename t,typename u> struct foo { void test(); template<typename v> void tester(v lhs); }; template<typename t,typename u> void foo<

我正在尝试向模板化类添加模板化方法。我提到了答案,但是语法不起作用。我添加了第二个方法,名为
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);
}

关于我为什么会出现此错误或我可能做错了什么,有什么建议吗?

请在下面更正的代码中插入注释:

#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);
}
#包括
模板
结构foo{
无效试验();
模板
孔隙测试仪(v-lhs);
};
模板
void foo::test(){

std::cout如果你想专门从事
测试仪
,那么请删除
类型名v
,并对
lhs
参数使用
int
。我想看看我是否可以为
测试仪
制作模板。如果以后我希望
lhs
类型为
字符串
,那你就不想这样做了部分专业化,那么为什么你有
tester
而没有
tester
?我不想部分专业化method tester。我想知道是否可以在模板化类中模板化一个方法?链接上说是,然后你需要删除
部分。首先,你从哪里获得
int
第二,您不能对成员函数进行部分专门化,所以您需要这样:
template foo::tester(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);
}