Minmal working example:
#include <iostream>
struct Printer
{
template<class T>
static void print(T elem) {
std::cout << elem << std::endl;
}
};
template<class printer_t>
struct Main
{
template<class T>
void print(T elem) {
// In this case, the compiler could guess T from the context
// But in my case, assume that I need to specify T.
printer_t::print<T>(elem);
}
};
int main()
{
Main<Printer> m;
m.print(3);
m.print('x');
return 0;
}
My compiler (g++) gives me the error "expected primary-expression before ‘>’ token". What's wrong and how to fix it?
C++11 accepted.
clang
gives a better error message in this case:
$ clang++ example.cpp -o example
example.cpp:18:20: error: use 'template' keyword to treat 'print' as a dependent template name
printer_t::print<T>(elem);
^
template
1 error generated.
Just add the template
where it says to, and you're set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With