C++ 为什么调用模板成员函数会出现错误?
因为C++ 为什么调用模板成员函数会出现错误?,c++,templates,compiler-errors,C++,Templates,Compiler Errors,因为t属于Test类型,其中Real是一个模板参数,所以t的类型实际上是一个依赖类型。您需要使用以下方法让编译器知道to是一个模板函数: t1.cpp: In constructor ‘Base<Real>::Base(const TST&)’: t1.cpp:20:15: error: expected primary-expression before ‘int’ t1.cpp:20:15: error: expected ‘;’ before ‘int’ _i=t.t
t
属于Test
类型,其中Real
是一个模板参数,所以t
的类型实际上是一个依赖类型。您需要使用以下方法让编译器知道to
是一个模板函数:
t1.cpp: In constructor ‘Base<Real>::Base(const TST&)’:
t1.cpp:20:15: error: expected primary-expression before ‘int’
t1.cpp:20:15: error: expected ‘;’ before ‘int’
_i=t.template to();
否则,您可能会尝试使用
运算符Now,它的to
是一个静态成员吗?好的,静态模板成员应该在模板结构或类名之后有template
关键字:Test::template to(…)
。当然,当从过于自由的MSVC移植到G++时,您才开始考虑它。C++是不适合你的,如果你喜欢晚上睡个好觉的话。
t1.cpp: In constructor ‘Base<Real>::Base(const TST&)’:
t1.cpp:20:15: error: expected primary-expression before ‘int’
t1.cpp:20:15: error: expected ‘;’ before ‘int’
_i = t.template to<int>();