visual-studio - When writing a binary tree in C++, using templates and setting friend classes makes an error.
迷茫
迷茫 2017-05-16 13:29:03
0
1
658

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! !

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
曾经蜡笔没有小新
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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template