Problem Description
As the title states, I have recently been learning data structures and implementing them in C++. I encountered the following problems when implementing a binary tree.
In the code in the book, the binary tree consists of two data structures BinaryTree
and BinartTreeNode
, and both use templates, as follows:
template <class T>
class BinaryTreeNode{
friend class BinaryTree<T>;
private:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
/*...其他...其他...其他...*/
}
template <class T>
class BinaryTree{
private:
BinaryTreeNode<T> root;
/*...其他...其他...其他...*/
}
But I encountered the following error when implementing it:
Sample code:
template <class T>
class BinaryTreeNode {
friend class BinaryTree<T>; /* 消除该句后可通过编译 */
private:
T info;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
};
template <class T>
class BinaryTree {
private:
BinaryTreeNode<T> *root;
};
mistake:
1>e:\it\c++\binarytree\binarytree\mybianrytree.h(20): error C2989: "BinaryTree": class template has been declared as a non-class template
error c2989
screenshot:
environment:
visual studio 2017 + win10 (mac dual system)
Sorry for the trouble, everyone! !
曾经蜡笔没有小新2017-05-16 13:31:03
template <class T> class BinaryTree; // 加一个前置声明
template <class T>
class BinaryTreeNode {
friend class BinaryTree<T>; /* 消除该句后可通过编译 */
private:
T info;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
};
template <class T>
class BinaryTree {
private:
BinaryTreeNode<T> *root;
};
ps: The mobile version of segmentfault is really like shit. After holding my nose and using it for so long, I realized that I can’t input the greater than or less than sign