在类函数中返回Struct指针

Returning a Struct pointer in a class function

本文关键字:Struct 指针 返回 类函数      更新时间:2023-10-16

我得到了一组错误,因为我的函数试图返回一个指向结构体的指针。有很多复杂的因素在我试图弄清楚,但这是类:

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以及其他类似的函数在基类中定义,因为我在派生类中没有看到它们的定义