C ++中的两个模板:“期望的主表达式之前”>“令牌” [英] Two templates in C++: "expected primary-expression before ‘>’ token"
本文介绍了C ++中的两个模板:“期望的主表达式之前”>“令牌”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最小工作示例:
#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){
//在这种情况下,编译器可以从上下文中猜出T
//但是在我的例子中,假设我需要指定T.
printer_t :: print< T>(elem);
}
};
int main()
{
Main< Printer> m;
m.print(3);
m.print('x');
return 0;
}
我的编译器(g ++)给我错误< '''token之前的表达式。发生了什么问题以及如何解决?
C ++ 11已接受。
解决方案>
clang
在此情况下会显示更好的错误讯息:
$ clang ++ example.cpp -o example
example.cpp:18:20:error:use'template'关键字将'print'视为依赖模板名称
printer_t :: print< T> elem);
^
模板
生成了1个错误。
只要添加模板
,您已设置。
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.
这篇关于C ++中的两个模板:“期望的主表达式之前”>“令牌”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文