James Franco
James Franco

Reputation: 4706

declaring a templated method inside a templated class

I am attempting to add a templated method to a templated class. I referred to this answer however the syntax is not working.I added the second method called tester which I would like to be templated. This is what I have

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);
}

I am getting the error for method tester this is the error that I get

  main.cpp:20:31: error: non-type partial specialization 'tester<v>' is not allowed
 void foo<t,u>::tester<v>(v lhs) {

Any suggestions on why i am getting this error or what I might be doing wrong ?

Upvotes: 1

Views: 102

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69854

Comment inline in the corrected code below:

#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);
}

Upvotes: 1

Related Questions