以下代码是一个类模板。在课堂之外,我定义了静态成员。但是,我使用两种方法来定义PrimeBlock *静态成员,方法 - 1使用模板别名,并报告重新声明错误;方法-2可以正常工作。我想知道为什么方法 - 1不起作用。
template <typename tree_type>
class Tree {
struct PrimeBlock {
vector<tree_type*> vec;
};
static tree_type* ROOT;
static PrimeBlock* primeBlock;
};
template <typename tree_type>
tree_type* ROOT = nullptr;
// method - 1 template alias for definition - error
template <typename tree_type>
using PrimeBlock = typename Tree<tree_type>::PrimeBlock;
template <typename tree_type>
PrimeBlock<tree_type>* Tree<tree_type>::primeBlock = nullptr; // this cannot work due to redeclaration error
// method - 2 - it works
//template <typename tree_type>
//typename Tree<tree_type>::PrimeBlock* Tree<tree_type>::primeBlock = nullptr; // however, this could work, why?
方法-1产生的错误消息
error: conflicting declaration ‘PrimeBlock<RtreeVariant>* Tree<RtreeVariant>::primeBlock’ PrimeBlock<RtreeVariant>* Tree<RtreeVariant>::primeBlock = nullptr; note: previous declaration as ‘Tree<RtreeVariant>::PrimeBlock* Tree<RtreeVariant>::primeBlock’ static PrimeBlock* primeBlock; error: declaration of ‘Tree<RtreeVariant>::PrimeBlock* Tree<RtreeVariant>::primeBlock’ outside of class is not definition [-fpermissive] PrimeBlock<RtreeVariant>* Tree<RtreeVariant>::primeBlock = nullptr;