在类函数中返回Struct指针
Returning a Struct pointer in a class function
我得到了一组错误,因为我的函数试图返回一个指向结构体的指针。有很多复杂的因素在我试图弄清楚,但这是类:
template <typename T>
class AVL : public BST<T>
{
public:
AVL(void) { head = NULL; }
~AVL();
virtual void insertEntry(T info);
virtual void deleteEntry(T info);
virtual bool isThere(T info);
protected:
struct t_node
{
string data;
t_node *L;
t_node *R;
};
t_node* head;
t_node* cPTR; //current pointer
t_node* pPTR; //parent pointer
int difference(t_node *tPTR);
int height(t_node *tPTR);
t_node* balance(t_node *tPTR);
//these functions can be read as:
//l2l_ROT = left to left rotation
//r2l_ROT = right to left rotation
void l2l_ROT(t_node *pPTR);
void r2r_ROT(t_node *pPTR);
void l2r_ROT(t_node *pPTR);
void r2l_ROT(t_node *pPTR);
};//end of class AVL
,这里是我得到错误的函数:
template <typename T>
t_node *AVL<T>::balance(t_node *tPTR)
{
int factor = difference(tPTR);
if( factor > 1)
{
if(difference(tPTR->L) > 0)
tPTR = ll_rotation(tPTR);
else
tPTR = lr_rotation(tPTR);
}
else if(factor < -1)
{
if(diff(tPTR->right) > 0)
temp = rl_rotation(tPTR);
else
temp = rr_rotation(tPTR);
}
return tPTR;
}
我为错误的间距道歉,我永远不知道如何正确地复制&粘贴我的代码。还有:这里是我得到的错误:
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C2143: syntax error : missing ';' before '*'
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C2065: 'T' : undeclared identifier
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C2923: 'AVL' : 'T' is not a valid template type argument for parameter 'T'
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C2065: 'avl_node' : undeclared identifier
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C2597: illegal reference to non-static member 'BST<T>::tPTR'
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C3867: 'BST<T>::tPTR': function call missing argument list; use '&BST<T>::tPTR' to create a pointer to member
1>c:usersjordindesktopattemptsattempt1consoleapplication1avl.h(75): error C2568: '*' : unable to resolve function overload
第一个错误,我对语法错误感到困惑,在此之前没有任何语法错误,我进行了三次检查。
编辑:在c++ visual studio 2012中,编译器甚至没有突出显示实际c++部分的"t_node",因为t_node是黑色的,没有链接到任何数据类型。有什么原因吗?
看来这个函数应该这样定义
template <typename T>
typename AVL<T>::t_node * AVL<T>::balance( typename AVL<T>::t_node *tPTR)
{
int factor = difference(tPTR);
if( factor > 1)
{
if(difference(tPTR->L) > 0)
tPTR = ll_rotation(tPTR);
else
tPTR = lr_rotation(tPTR);
}
else if(factor < -1)
{
if(diff(tPTR->right) > 0)
temp = rl_rotation(tPTR);
else
temp = rr_rotation(tPTR);
}
return tPTR;
}
假设变量temp
和函数rr_rotation
, ll_rotation
以及其他类似的函数在基类中定义,因为我在派生类中没有看到它们的定义
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- "typedef"类型名称是否可以像"struct"定义那样声明指向结构的指针?
- sizeof(* struct 指针)是否给你结构的值
- 删除字符串指针在struct中的内存
- 如何使用struct构造函数在结构中初始化指针阵列
- 引用绑定到类型 'struct value_type' 的 null 指针
- 指向struct的指针,包含指向对象的指针,我想调用一个函数
- 如何从采用 int 和 struct 类型的指针的函数中取消引用指针返回的地址?
- 将指向struct的指针强制转换为数组
- 在struct[]中分配char*指针值(结构成员)的最优化方法
- 在类函数中返回Struct指针
- typedef struct 指针进入 JNA
- 将char复制到struct指针char
- 指针函数在类中返回Struct的值
- 初始化Struct指针
- 将指向struct的指针强制转换为数组指针是否有效?
- 自动更新struct中的指针值
- 初始化指向struct语法的指针
- 在c++中初始化指向struct的指针