[英]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:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.h(75): error C2143: syntax error : missing ';' before '*'
1>c:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.h(75): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.h(75): error C2065: 'T' : undeclared identifier
1>c:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.h(75): error C2923: 'AVL' : 'T' is not a valid template type argument for parameter 'T'
1>c:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.h(75): error C2065: 'avl_node' : undeclared identifier
1>c:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.h(75): error C2597: illegal reference to non-static member 'BST<T>::tPTR'
1>c:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.h(75): error C3867: 'BST<T>::tPTR': function call missing argument list; use '&BST<T>::tPTR' to create a pointer to member
1>c:\users\jordin\desktop\attempts\attempt1\consoleapplication1\avl.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
和其他類似函數,因為在此派生類中看不到它們的定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.