【问题标题】:reference to pointer not working on header files in C++引用指针在 C++ 中对头文件不起作用
【发布时间】:2014-06-10 03:02:50
【问题描述】:

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

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

【问题讨论】:

  • 听起来您缺少 Node 类型的声明。
  • 我编辑了我有头文件的声明。我可以毫无问题地编译 .cc 文件。但是头文件给了我一个错误。
  • 发布的头文件文本中没有出现有问题的声明。
  • 我们没有在您的标题中看到free_memory_circle。请显示所有相关代码。
  • 错误:当我尝试编译 .h 文件时,应在“&”标记之前使用“;”、“,”或“)”

标签: c++ linked-list pass-by-reference forward-declaration


【解决方案1】:

我们仍然没有你所有的头文件,但我猜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 );

【讨论】:

  • 谢谢。我设法解决了它。前向声明存在问题,而且我的 IDE 也弄乱了头文件,它正在使用 g++ 使用 gcc 和 .cc 文件编译它们。 。对不起,我不能投票给你,因为我的声望较低。
  • 你根本不应该自己编译头文件(除非你在谈论预编译的头文件)
  • @KrvPerera 如果这解决了您的问题,您应该单击复选标记图标(在投票箭头下方)。
猜你喜欢
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 2010-10-23
相关资源
最近更新 更多