Dim*_*tic 1 c++ templates compiler-errors
这是我无法编译的一些代码的精简版本。
template <typename name_of_type_t> class classA_t
{ /* etc etc etc */ };
template <typename name_of_type_t> class classB_t
{
public:
classA_t<name_of_type_t> && return_new();
};
classA_t<name_of_type_t> && classB_t::return_new() // <-- errors here
{
classA_t<name_of_type_t> * S = new classA_t<name_of_type_t>;
return std::move(*S);
}
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)
我得到的编译器错误是
template_error.cpp:12:10: error: use of undeclared identifier 'name_of_type_t'
classA_t<name_of_type_t> && classB_t::return_new()
^
template_error.cpp:12:29: error: 'classB_t' is not a class, namespace, or enumeration
classA_t<name_of_type_t> && classB_t::return_new()
^
template_error.cpp:4:42: note: 'classB_t' declared here
template <typename name_of_type_t> class classB_t
^
2 errors generated.
Run Code Online (Sandbox Code Playgroud)
如果name_of_type_t未声明,为什么编译器没有标记我之前对它的使用?classB_t如果不是类,编译器会认为是什么?
classB_t是一个模板。在类定义之外声明其成员函数时,需要再次声明模板参数列表,并在类类型中指定模板参数,例如
template <typename name_of_type_t>
classA_t<name_of_type_t> && classB_t<name_of_type_t>::return_new()
{
classA_t<name_of_type_t> * S = new classA_t<name_of_type_t>;
return std::move(*S);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |