最佳工作示例:
#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;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器(g ++)给了我错误"在'>'令牌之前预期的primary-expression".怎么了?怎么解决?
C++ 11接受了.
Car*_*rum 11
clang 在这种情况下给出了更好的错误消息:
$ 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.
Run Code Online (Sandbox Code Playgroud)
只需添加template它所说的位置,就可以了.
| 归档时间: |
|
| 查看次数: |
1099 次 |
| 最近记录: |