Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对C+;中不在头文件上工作的指针的引用+;_C++_Linked List_Pass By Reference_Forward Declaration - Fatal编程技术网

C++ 对C+;中不在头文件上工作的指针的引用+;

C++ 对C+;中不在头文件上工作的指针的引用+;,c++,linked-list,pass-by-reference,forward-declaration,C++,Linked List,Pass By Reference,Forward Declaration,当我在头文件中使用*&引用时,它会给我一个错误 void free_memory_circle( Node *& root ); error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token 我有一个工作列表文件,所有函数都在一个文件中,当我试图将它们分离为.h和.cc文件时,我不能使用*&引用指针。有人能给我解释一下原因吗 我的头文件已编辑 #ifndef _EX4A_FREEMEMORY_H_ #define _EX4A_FREEME

当我在头文件中使用*&引用时,它会给我一个错误

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.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);

}
}
自由记忆

#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
包含
freemory.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 );

/#include“BST.h”听起来好像缺少节点类型的声明。我编辑了我在头文件中有声明。我可以编译.cc文件,没有任何问题。但是头文件给了我一个错误。有问题的声明没有出现在发布的头文件文本中。我们在你的头文件中没有看到
free\u memory\u circle
。请显示所有相关代码。错误:应为“;”、“,”当我试图编译.h文件时,在“&”标记之前加上“'),谢谢。我设法解决了它。转发声明有一个问题,而且我的IDE会弄乱头文件,因为它是用gcc和.cc文件由g++。很抱歉,我不能投票给你,因为我的声誉不太好。你根本不应该自己编译头文件(除非你说的是预编译头)@KrvPerera如果这解决了你的问题,你应该单击复选标记图标(投票箭头下方)。
#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 "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 );