1

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
Krv Perera
  • 119
  • 2
  • 15

1 Answers1

3

We still don't have all your header files, but I'm guessing that Circle.h includes FreeMemory.h, causing the problematic declaration to be parsed before the declaration of struct Node.

You can break the circular dependency (if there really is one) with a forward declaration of Node instead of including BST.h, from which you only need this one small item.

// #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 );
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Thank you. I managed to solve it. There was a issue with forward declaration and also my **IDE was messing with header files that it was compiling them with gcc and .cc files by g++.** . Sorry I can't vote you, because I have less reputation. – Krv Perera Jun 10 '14 at 11:59
  • You shouldn't be compiling header files by themself at all (unless you're talking about precompiled headers) – M.M Jun 10 '14 at 12:55
  • @KrvPerera You should click the checkmark icon (below the vote arrows) if this solved your problem. – Potatoswatter Jun 10 '14 at 13:46