class 模板构造函数中对 'Position<int>::treeHeight' 的未定义引用
Undefined Reference to 'Position<int>::treeHeight' in class template constructor
我被困在这里了。我收到的错误提示我只有 treeHeight 有一个未定义的引用。在使用 treeHeight 的构造函数和成员函数定义中。我可以在模板化 class 中没有静态变量吗?
template <class T> // base element type
class Position // a node position
{
public:
Position(const int& HEIGHT) { treeHeight = HEIGHT; }
PositionList<T> children() const; // (0) left, (1) duplicate, (2) right
bool isRoot() const { return parent() == nullptr; }
bool isExternal() const { return leftNode == nullptr
&& rightNode == nullptr; }
void incDepth();
/* Getters */
T operator*() const { return data; } // get element
Position<T> * parent() const { return parentNode; }
// Position<T> * duplicate() const { return duplicNode; }
// Position<T> * left() const { return rightNode; }
// Position<T> * right() const { return leftNode; }
friend void BTClass<T>::insert(Position<T> *, const T&);
private:
Position<T> * parentNode {nullptr};
Position<T> * leftNode {nullptr}; // Less than parent
Position<T> * duplicNode {nullptr}; // Equal to parent
Position<T> * rightNode {nullptr}; // Greater than parent
T data {T()};
int depth {0}; // Depth of current node within tree. (cannot exceed height)
static int treeHeight;
};
我想通了!我正在声明构造函数但没有定义它。我已经在私有变量部分定义了变量,因此,删除构造函数允许编译器创建自己的构造函数。
我被困在这里了。我收到的错误提示我只有 treeHeight 有一个未定义的引用。在使用 treeHeight 的构造函数和成员函数定义中。我可以在模板化 class 中没有静态变量吗?
template <class T> // base element type
class Position // a node position
{
public:
Position(const int& HEIGHT) { treeHeight = HEIGHT; }
PositionList<T> children() const; // (0) left, (1) duplicate, (2) right
bool isRoot() const { return parent() == nullptr; }
bool isExternal() const { return leftNode == nullptr
&& rightNode == nullptr; }
void incDepth();
/* Getters */
T operator*() const { return data; } // get element
Position<T> * parent() const { return parentNode; }
// Position<T> * duplicate() const { return duplicNode; }
// Position<T> * left() const { return rightNode; }
// Position<T> * right() const { return leftNode; }
friend void BTClass<T>::insert(Position<T> *, const T&);
private:
Position<T> * parentNode {nullptr};
Position<T> * leftNode {nullptr}; // Less than parent
Position<T> * duplicNode {nullptr}; // Equal to parent
Position<T> * rightNode {nullptr}; // Greater than parent
T data {T()};
int depth {0}; // Depth of current node within tree. (cannot exceed height)
static int treeHeight;
};
我想通了!我正在声明构造函数但没有定义它。我已经在私有变量部分定义了变量,因此,删除构造函数允许编译器创建自己的构造函数。