gpt4 book ai didi

c++ - 引用指针不适用于 C++ 中的头文件

转载 作者:行者123 更新时间:2023-11-30 02:45:50 25 4
gpt4 key购买 nike

当我在头文件中使用 *& 引用时出现错误

void free_memory_circle( Node  *& root );

error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

我有一个工作喜欢的列表文件,所有功能都在一个文件中,当我尝试将它们分成 .h 和 .cc 文件时,我无法使用 *& 引用指针。谁能解释一下原因。

我的头文件已编辑

#ifndef _EX4A_FREEMEMORY_H_
#define _EX4A_FREEMEMORY_H_

#include "BST.h"


void free_memory_circle( Node *&root );
void free_memory_rectangle( Node *&root );



#endif // FreeMemory.h

我的代码文件

#include<cstdlib>
#include "FreeMemory.h"
#include "Circle.h"
#include "Rectangle.h"

void free_memory_circle( Node *&root )
{
if( root == NULL ) return;
free_memory_circle( root->_left );
free_memory_circle( root->_right );
delete( (Circle * )root->data);
delete ( root);

}

void free_memory_rectangle( Node *&root )
{
if( root == NULL ) return;

free_memory_rectangle( root->_left );
free_memory_rectangle( root->_right );
delete( (Rectangle * )root->data);
delete ( root);

}

BST.h文件

#ifndef _EX4A_BST_H_
#define _EX4A_BST_H_
#include "Circle.h"
#include "Rectangle.h"

typedef struct Node
{
void *data;
struct Node *_left, *_right;
} Node ;

extern double max_radius; //saves maximum radius
extern double max_area_rect; //saves maximum area of rectangles
extern Node *BST_circles ;
extern Node *BST_rectangles ;

//this is a similar to linked list(binary search tree)
//so I have to use *& here for head node
//void add_circle_BST( Node *& root , Circle *p );
void add_circle_BST( Node * root , Circle *p );
void PrintBST_circle_inorder( Node *root );
void add_rectangle_BST( Node *root , Rectangle *p );
void PrintBST_Rectangle_inorder( Node *root );
void find_max_radius( Node *root);
void find_max_area_rect( Node *root);

#endif // BST.h

BST.cc文件

#include<iostream>
#include "BST.h"
#include "Rectangle.h"
#include "Circle.h"

using namespace std;

double max_radius = 0; //saves maximum radius
double max_area_rect = 0; //saves maximum area of rectangles

Node *BST_circles = NULL;
Node *BST_rectangles = NULL;

//this is a similar to linked list(binary search tree)
//so I have to use *& here for head node
void add_circle_BST( Node *&root , Circle *p )
{
//root = BST_circles;
if( !root ) //IF root is null
{
root = new Node;
root->data = p;
}else{
Circle *temp = (Circle *)root->data;
if( temp->_x > p->_x){ //if x value less than root x
add_circle_BST(root->_left , p); //add to left
}else{
add_circle_BST( root->_right , p);
}
}
}

void PrintBST_circle_inorder( Node *root )
{
if( root ){
PrintBST_circle_inorder( root->_left);
Circle *temp = (Circle *)root->data;
cout << temp->_x << " " << temp->_y << " "
<< temp->_r << endl;
PrintBST_circle_inorder( root->_right);

}

}


void add_rectangle_BST( Node *root , Rectangle *p )
{
if( !root ){ //IF root is null
root = new Node;
root->data = p;
}else { //
Rectangle *temp = (Rectangle *)root->data;
if( temp->_top_left_x > p->_top_left_x)//if x value less than root x
{
add_rectangle_BST(root->_left , p); //add to left
}
else
{
add_rectangle_BST( root->_right , p);
}
}
}


void PrintBST_Rectangle_inorder( Node *root )
{
if( root )
{
PrintBST_Rectangle_inorder( root->_left);
Rectangle *temp = (Rectangle *)root->data;
cout << temp->_top_left_x << " " << temp->_top_left_y << " "
<< temp->_bot_right_x << " " << temp->_bot_right_y << endl;
PrintBST_Rectangle_inorder( root->_right);

}

}



void find_max_radius( Node *root)
{
if( root ){ //IF root is null
Circle *temp = (Circle *)root->data;
if( max_radius < temp->_r )
{
max_radius = temp->_r;
}
find_max_radius(root->_left ); //add to left
find_max_radius( root->_right);

}
}

void find_max_area_rect( Node *root)
{
if( root ){ //IF root is null
Rectangle *temp = (Rectangle *)root->data;
if( max_area_rect < Area_rect( temp ) )
{
max_area_rect = Area_rect( temp );
}
find_max_area_rect(root->_left ); //add to left
find_max_area_rect( root->_right);

}
}

圆.h

#ifndef _EX4A_CIRCLE_H_
#define _EX4A_CIRCLE_H_


typedef struct Circle{
double _x,_y,_r;
} Circle;

#endif // Circle.h

FreeMemory.h

#ifndef _EX4A_FREEMEMORY_H_
#define _EX4A_FREEMEMORY_H_

#include "BST.h"

void free_memory_circle( Node *&root );
void free_memory_rectangle( Node *&root );

#endif // FreeMemory.h

最佳答案

我们仍然没有你所有的头文件,但我猜测 Circle.h 包含 FreeMemory.h,导致有问题的声明在之前被解析struct Node 的声明。

您可以通过 Node 的前向声明而不是包含 BST.h 来打破循环依赖(如果确实存在的话),从中您只需要这个小元素。

// #include "BST.h" <= remove this

struct Node; // Introduce Node as a placeholder, or "incomplete class."

void free_memory_circle( Node *&root );
void free_memory_rectangle( Node *&root );

关于c++ - 引用指针不适用于 C++ 中的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24132190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com