所以,我想让代码更漂亮一点,因为我希望类 def 只包含函数声明。这是我的代码:
template <typename T>
class splay_tree
{
struct node
{
T data;
node* parent;
node* left;
node* right;
};
node* grandp(node* x)
{
return NULL;
}
node* error();
};
node* splay_tree<T>::error()
{
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何将grandp实现移到splay tree定义之下。请注意,它的返回类型是node*,所以 gcc 给我带来了困难。是否可以?
像这样
template <class T>
typename splay_tree<T>::node* splay_tree<T>::error()
Run Code Online (Sandbox Code Playgroud)
方法的返回类型不是方法的范围,因此splay_tree<T>::必须单独指定。
另外,由于这是一个模板,您需要 template <class T>
你已经得到了答案,但我会提出 Evg 的想法,即使用尾随返回类型splay_tree<T>::node引入范围以显示该替代方案:
template <typename T>
class splay_tree
{
struct node
{
T data;
node* parent;
node* left;
node* right;
};
node* grandp(node*);
node* error();
};
template<typename T>
auto splay_tree<T>::grandp(node* x) -> node* // trailing return type
{
return nullptr;
}
template <class T>
auto splay_tree<T>::error() -> node* // trailing return type
{
return nullptr;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |