I have a template-structure that I want to “overload” like this:
#include <iostream>
template <typename T, typename U = int>
struct foo {
void operator()(T, U);
}
template <typename T, typename U = int>
void foo::operator()(T a, U b){
std::cout << "T, U ()\n";
}
template <typename T>
struct foo<T, int> {
void operator()(T);
}
template <typename T>
void foo<T, int>::operator()(T a){
std::cout << "T ()\n";
}
int main(int argc, char **argv){
foo<int> a;
foo<int, char> b;
a(1);
b(2, 'b');
return false;
}
But on compiling I get the following error:
($ g++ test.cpp -o test)
test.cpp:11:6: error: 'template<class T, class U> struct foo' used without template parameters
test.cpp:11:30: error: 'void operator()(T, U)' must be a nonstatic member function
This is strange since the definition of foo< T, int >::operator() seems to work perfectly. Also, when I define the function inline like this:
template <typename T, typename U = int>
struct foo {
void operator()(T a, U b){ std::cout << "T, U ()\n"; }
}
It works without problems.
You have to use those template parameters to specify which foo,
foo<T,U>::operator(). And remove the default template parameter value from the definition.You also forgot the semicolons after the template class definitions.