visual-studio - C++编写二叉树时,使用模板,设置友类出错。
本文介绍了visual-studio - C++编写二叉树时,使用模板,设置友类出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
问题描述
如题,最近在学习数据结构在用C++来实现,在实现二叉树的时候遇到了如下的问题。
书中的代码里面,二叉树由两个数据结构组成BinaryTree
和BinartTreeNode
,并且两个都用到了模板,如下:
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;
/*...其他...其他...其他...*/
}
但是我在实现的时候遇到了如下的错误:
示例代码:
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;
};
错误:
1>e:\it\c++\binarytree\binarytree\mybianrytree.h(20): error C2989: BinaryTree: 类 模板 已经声明为非类 模板
截图:
环境:
visual studio 2017 + win10(mac 双系统下的)
麻烦各位了!!
解决方案
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:segmentfault手机版真的跟屎一样啊,捏着鼻子用了这么久,才发现居然不能输入大于号小于号
这篇关于visual-studio - C++编写二叉树时,使用模板,设置友类出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文