C++ 指针返回中的编译错误
我的BST课程和你的一样 BST.hppC++ 指针返回中的编译错误,c++,pointers,binary-search-tree,C++,Pointers,Binary Search Tree,我的BST课程和你的一样 BST.hpp template<class T> class BinarySearchTree { private: struct tree_node { tree_node* left; tree_node* right; T data; tree_node( const T & thedata, tree_node * l = NULL, tree_node * r = NULL )
template<class T>
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
T data;
tree_node( const T & thedata, tree_node * l = NULL, tree_node * r = NULL )
: data( thedata ), left( l ), right( r ) { }
};
tree_node* root;
public:
//some functions
private:
struct tree_node* minFunc( tree_node** node);
};
模板
类二进制搜索树
{
私人:
结构树节点
{
树节点*左;
树节点*右;
T数据;
树节点(常量T和数据,树节点*l=NULL,树节点*r=NULL)
:数据(thedata),左(l),右(r){
};
树节点*根;
公众:
//一些功能
私人:
结构树节点*minFunc(树节点**节点);
};
我试图从函数中返回指针,如中所述
minFunc的定义位于同一BST.hpp文件中
template <class T>
struct tree_node* BST<T>::minFunc(tree_node** node)
{
tree_node* current = *node;
while(current->left != NULL)
{
current = current->left;
}
return current;
}
模板
结构树节点*BST::minFunc(树节点**节点)
{
树节点*当前=*节点;
while(当前->左!=NULL)
{
当前=当前->左侧;
}
回流;
}
无法找出编译错误:
错误C2143:语法错误:缺少“;”在“*”之前
错误C2065:“T”:未声明的标识符
错误C2955:“BST”:使用类模板要求模板参数列表
错误C2509:“minFunc”:成员函数未在“BST”中声明
所有这些都指向定义我的最佳猜测是,
结构树节点
不可见。它可能没有在某个类中声明。我最好的猜测是,结构树节点
不可见。它可能没有在某个类中声明/声明。treenode是BST中的私有结构-您不能在BST外部访问它。treenode是BST中的私有结构-您不能在BST外部访问它
tree_node* minFunc( tree_node** node);
相应地更改它的定义
template <class T>
typename BST<T>::tree_node* BST<T>::minFunc(tree_node** node)
{
tree_node* current = *node;
while(current->left != NULL)
{
current = current->left;
}
return current;
}
模板
类型名BST::树节点*BST::minFunc(树节点**节点)
{
树节点*当前=*节点;
while(当前->左!=NULL)
{
当前=当前->左侧;
}
回流;
}
顺便说一下,请注意方法minFunc是私有的,不能在类之外访问它
tree_node* minFunc( tree_node** node);
相应地更改它的定义
template <class T>
typename BST<T>::tree_node* BST<T>::minFunc(tree_node** node)
{
tree_node* current = *node;
while(current->left != NULL)
{
current = current->left;
}
return current;
}
模板
类型名BST::树节点*BST::minFunc(树节点**节点)
{
树节点*当前=*节点;
while(当前->左!=NULL)
{
当前=当前->左侧;
}
回流;
}
顺便说一句,注意方法minFunc是私有的,不能在类之外访问它可能是缺少的;拧紧所有部件。您是否已将
BST
的头文件包含在.cpp?@BlackBear中?没有丢失的代码>,编译器有误导性。@节点,因为它是一个模板类,所有代码都在同一个.hpp文件中,可能缺少;拧紧所有部件。您是否已将BST
的头文件包含在.cpp?@BlackBear中?没有丢失的代码>,编译器有误导性。@节点,因为它是一个模板类,所有代码都在同一个.hpp文件中。它是类BST的私有成员,整个代码都在.hpp文件中。这仍然是个问题吗?它是类BST的私有成员,整个代码都在.hpp文件中。这仍然是一个问题吗?删除struct给出了C4430的定义@2.我已将所有内容都放在同一个.hpp文件中。@Kaushik您是否将tree\u节点的定义放在类BinarySearchTree之前?tree\u节点的定义放在类BinarySearchTree中。我已将代码放入线程中。正确。1.我在函数定义中遗漏了类型名。2.关于私有成员,我的意图与此线程类似。删除struct会给出C4430的定义@2.我已将所有内容都放在同一个.hpp文件中。@Kaushik您是否将tree\u节点的定义放在类BinarySearchTree之前?tree\u节点的定义放在类BinarySearchTree中。我已将代码放入线程中。正确。1.我在函数定义中遗漏了类型名。2.关于私人会员,我的意图与此类似。