- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在头文件中使用 *& 引用时出现错误
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/
我有一位客户希望她的网站适合 iPad/iPhone。该网站有一个用 Flash 制作的菜单标题。我使用 Google 的 Swiffy 将文件转换为 flash_1.html 文件。 当网站检测到
我注意到 boost 库使用了 (.hpp) 的头文件。 我很好奇,因为我看到的大多数源文件都使用普通的 .h 头文件。 是否有任何特殊情况需要使用 .hpp 而不是 .h ? 谢谢 最佳答案 只是约
C需要不同的头文件,如stdio.h、stdlib.h、fcntl.h等,对于不同的函数和结构定义,这是为什么?在幕后,一切都归结为 libc 那么为什么不创建一个包含所有定义和原型(prototyp
我是一名工科学生,不是一个非常强大的程序员。我的一项作业包括使用 openGL 创建 VR 程序。我得到了一个使用 gmtl 的模板(我真的不想重写)标题广泛。该作业需要实现一些 sixense模拟中
我正在尝试编译涉及C和C++文件的应用程序。使用一个特定的 header ,我遇到了问题。有问题的文件(C++头文件)如下所示: #ifndef TASK_H #define TASK_H #incl
我编写了我的项目,将main和c源代码保存在一个文件中,并将头文件保存在codeblocks的include目录中。当我从项目主函数中调用我的函数时,它编译得很好......但是当我将 header
我正在尝试将 .so 库导入到 python 代码中以使用 c 函数。我认为使用 from ctypes import * import ctypes lib = CDLL('./libcaenhvw
我有 2 个重叠的头文件,如下所示: header1.h ... __declspec(dllexport) void abc(); __declspec(dllexport) void xyz(
我有一个大型 Java 库,我想开发几个与该库接口(interface)的较小应用程序。该库将作为 JAR 出现在目标设备的类路径中,但我想尽可能避免在编译时出现整个库(JAR 或源代码)。 (如果重
我必须在我的项目中使用相机制造商提供的库。我正在使用 Visual Studio 2015。我在编写#include 后合并了VS 2015 建议的所有头文件。 我已经在VS 2015中指定了包含文件
我在编写我的项目时遇到了这个问题。我有 2 个 header ,每个 header 都有一个类,它们需要另一个,如下所示。 我以为这只是需要使用前向声明,但仍然不起作用。我没有想法。 寻求帮助:D H
我在一个项目中工作,我想在C++中做反射,所以经过研究我发现最好的方法是解析头文件以获得XML格式的抽象语法树并在反射中使用它。我尝试了很多工具,但没有一个与 visual c++ 2008 或 vi
我尝试从 BufferedImage 制作一个 BMP 文件。这是我尝试在 bmp 文件中写入标题和像素的函数。 我有一个错误,但我找不到那个。我需要你的帮助。 static void writeTo
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: undefined reference to `WinMain@16' 我一直在研究循环双链表。决定创建一个
处理这种情况的最佳做法是什么? class A { private: std::vector derp; public: struct B { ...
上下文:我正在为嵌入式板构建一些代码。它需要安装 Xilinx 工具、Linaro 工具链,然后调用开发板构建目录中的设置 bash 脚本(我们称之为 setup.sh)。 如果我不运行 setup.
我尝试使用头文件和源文件,但遇到了问题。因此,我对我正在尝试做的事情做了一个简化版本,我在 CodeBlocks 中遇到了同样的错误(undefined reference to add(double
当我包含用于将某些程序的整数类型转换为字符串类型的#include 头文件时,我的编译器(GCC for C++)抛出错误。谁能帮我解决这个问题? 这是一个C++的小代码,我是第一次尝试。 #incl
我的头文件中有一些错误,我不知道如何修复,因为我是 C++ 的新手。 这是头文件的代码: #pragma once typedef unsigned int uint; class DCEncrypt
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是一名优秀的程序员,十分优秀!