c++ - 为什么我不能在我的代码中使用“余额”?
问题描述
不知道怎么回事,系统显示:
使用未声明的标识符“余额”。
但它应该可以工作,因为“l”和“r”可以工作。???为什么?我用正确的方式使用余额,但它仍然无法使用?顺便说一句,这是一棵 AVL 树,“大学信息”是请求的一部分,但不是重要的部分。
struct uninfo ; // university information
struct uninfo {
string all;
string schoolName;
string subjectName;
string day;
string grade;
int nStudent;
int nTeacher;
int nGraduate;
} ;
struct tree
{
int key ;
vector<uninfo> list ;
tree *left ;
tree *right ;
//tree *root ;
int height ; // tree height
}; // related to the data
class data {
int total ;
int size ;
string fileNum ;
ifstream rFile ;
ofstream wFile ;
public:
int getheight( tree *N )
{
if ( N == NULL)
return 0;
return N -> height ;
}
//data(){ root = NULL ; }
//tree avltree ;
tree *root ;
vector<uninfo> InputList ;
bool ReadFile( ) ;
void clear() ;
void list() ;
bool SaveData() ;
}; // for data sorting
tree* r( tree *parent ) { // this is the right spin
tree *z = new tree() ; // create a new node
tree *x = new tree() ; // create a new node
tree tmp ;
z = parent -> left ; // set z as the parent's left child
x = z -> right ; // set x as z's right child
parent -> left = x ; // set the parent's left child as x
z -> right = parent ; // set z's right child as the parent
parent -> height = max( getheight( parent -> left ), getheight( parent -> right ) ) + 1 ; // update the height
z -> height = max( getheight( z -> left ), getheight( z -> right ) ) + 1 ; // update the height
return z ; // return z
} // end of right right spin
tree* l( tree *parent ) { // this is the leftspin
parent = new tree() ;
tree *z = new tree() ; // create a new node
tree *x = new tree() ; // create a new node
z = parent -> right ; // set the z as the current parent's right child
x = z -> left ; // set x as z's left child
parent -> right = z ; // set parent's right child as z
z -> left = parent; // set z's left child as the parent
z -> height = max( getheight( z -> left ), getheight( z -> right ) ) + 1 ; // update the height
parent -> height = max( getheight( parent -> left), getheight( parent -> right ) ) + 1 ; // update the height
return z ; // return z
}// end of the left spin
tree *insert( tree* v, int y )
{
data tmp ;
// data tmp ; in order to access the struct in class, i have to create a new data
if ( tmp.root == nullptr ) // if root == null
{
return ( createnode( v -> list.at( y ).nGraduate ) ); // return a int value
} // if()
if ( v -> list.at( y ).nGraduate > ( tmp.root -> list.at( y ).nGraduate ) ) // if the num is bigger then the num in root
{
tmp.root -> right = insert( tmp.root -> right, v -> list.at( y ).nGraduate ) ; //set the roo's right child as ( put into function createavltree again ) value
tmp = balance( v , y ) ;
// this is the code that went wrong
//error issue : Use of undeclared identifier 'balance'
} // if()
else if ( v -> list.at( y ).nGraduate == ( tmp.root -> list.at( y ).nGraduate ) ) // if the num == num in root
{
return tmp.root ; // return root
} // else if(), this condition is not allowed
else
{
tmp.root -> left = insert( tmp.root -> left, tmp.InputList.at( y ).nGraduate ) ; // set root's left child as ( put into funtion createavltree again ) value
} // else
tmp.root -> height = 1 + max( tmp.getheight( tmp.root -> left ), tmp.getheight( tmp.root -> right ) ) ; // update the height
return v ;
} // end of insert
tree *balance( tree *tmp, int y )
{
int f = 0 ;
data t ;
f = getheight( t.root -> left ) - getheight( t.root -> right ) ;
if ( f > 1 ) // if the factor > 1
{
if ( ( t.root -> left -> list.at( y ).nGraduate ) > t.InputList.at( y ).nGraduate ) // if the data currently put in is smaller than the current node's left child
{
return r( t.root ) ; // do the ll spin
} // if(), this is the ll spin
else if ( t.InputList.at( y ).nGraduate > ( t.root -> left -> list.at( y ).nGraduate ) ) // if the data currently put in is bigger than the current node's left child
{
t.root -> left = l( t.root -> left ) ; // do a left spin first
return r( t.root ) ; // then do a right spin
} // else if(), this is the lr spin
} // if()
else if ( -1 > f ) // if -1 > the facor
{
if ( t.InputList.at( y ).nGraduate > ( t.root -> right -> list.at( y ).nGraduate ) ) // if the data currently put in is bigger than the current node's right child
{
return r( t.root ); // do the rr spin
} // if(), this is thr rr spin
else if ( ( t.root -> right -> list.at( y ).nGraduate ) > t.InputList.at( y ).nGraduate) // if the data currently put in is smaller than the current node's right child
{
t.root -> right = r( t.root -> right ) ; // do the right spin first
return l( t.root ) ; // then do a left spin
} // else if(), this is the rl spin
} // else if()
return tmp ;
} // end of balance
解决方案
你必须在使用之前声明任何东西,这是 C++ 的基本规则。你balance
在声明之前使用它,所以这是不允许的。
要声明它,请添加一个函数原型
tree *balance( tree *tmp, int y );
把它放在声明之后,tree
但在第一次使用balance
. 在声明之后data
但在函数r
之前似乎是一个好地方。
l
并且r
没关系,因为您在使用它们之前定义了这些函数,定义算作声明。但是为l
and添加原型也没有什么坏处r
。
推荐阅读
- python - 如何使用 Anaconda 安装特定版本的 Python?
- reactjs - React Redux:如何找到 Redux 中调用的最后一个操作并再次触发相同的操作
- c# - 字符串 [] 与列表的幕后 foreach
- matplotlib - 在循环中添加新轴后重绘 plt 图
- c++ - 这个随机字符串打印代码有什么问题?
- cpu - 如果一个微处理器和总线有不同的位系统,那么微处理器系统总线周期是多少(续)
- azure-databricks - 从 EventHub 读取并写入 delta Lake 时找不到 sourceVersion 错误
- javascript - Javascript:检测何时添加了不可检索的 src 的 iframe
- javascript - .onclick() 仅在第一个按钮上触发
- ios - iOS 13 - UIBarButtonItem 尺寸太小