When I use *& reference in header files it gives me a error
void free_memory_circle( Node *& root );
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
I have a working liked list file with all the functions in a one file and when I try to separate them into .h and .cc files I cannot use *& reference pointer. Would someone explain me the reason.
my header file edited
#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
my code file
#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 file
#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 file
#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);
}
}
Circle.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