Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么调用模板成员函数会出现错误?_C++_Templates_Compiler Errors - Fatal编程技术网

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>();