【问题标题】:I receive a compiling error while trying to compile my code尝试编译代码时收到编译错误
【发布时间】:2019-04-18 17:18:51
【问题描述】:

我正在为我的学期项目创建一个程序,该程序采用 .pbm 格式的位图图像,通过贝塞尔曲线对其进行简化并以 .eps 格式输出结果。大学建议我们使用可变大小的数组进行项目,并且已经给出了声明。

二次贝塞尔可变大小数组的结构是:

typedef struct TTV_Bezier2_
{
  UINT nb;
  UINT cap;
  UINT taille_elt;
  Bezier2 * tab;
}TTV_Bezier2;

UINT 是大学给出的声明(typedef unsigned int) Bezier2 是我创建的一种类型。这是我的程序中由 3 个控制点定义的二次贝塞尔曲线的表示:

typedef struct Bezier_q
{
    Point C0;
    Point C1;
    Point C2;
}Bezier2;

Point 是另一个 typedef 结构体:

typedef struct Point_ {
    double x,y;
} Point;

这是我的问题:可变大小的 Bezier 数组在名为 TTV_Bezier.h 的文件中定义,而 Bezier2 类型在名为 bezier.h 的文件中定义。我已将这些文件相互包含,因为它们每个文件都使用彼此的这些类型。但是在编译时出现以下错误:

gcc -c -g -O2 -Wall -I. test_simplification_bezier2.c

In file included from TTV_Bezier2.h:8,
  from test_simplification_bezier2.c:8:
  bezier.h:91:1: error: unknown type name ‘TTV_Bezier2’; did you mean ‘Bezier2’?
  TTV_Bezier2 * simplification_douglas_peucker_bezier2(TTV_Point CONT, int j1, int j2, double d);

我被告知 Makefile 中的依赖项存在问题,但我不这么认为,因为我已经将它们包含在正确的文件中。你能帮帮我吗?

文件:

TTV_Bezier2.h:

#ifndef _TTV_BEZIER2_H_
#define _TTV_BEZIER2_H_

#include <stdio.h>
#include <stdlib.h>

#include "types_macros.h"
#include "bezier.h"

typedef struct TTV_Bezier2_
{
    UINT nb;
    UINT cap;
    UINT taille_elt;
    Bezier2 * tab;
}TTV_Bezier2;

TTV_Bezier2 * creer_TTV_Bezier2_vide();

Bezier2 element_TTV_Bezier2(TTV_Bezier2 T, UINT i);

UINT nb_elements_TTV_Bezier2(TTV_Bezier2 * T);

TTV_Bezier2 * ajouter_element_TTV_Bezier2(TTV_Bezier2 * T, Bezier2 e);

TTV_Bezier2 * concatener_TTV_Bezier2(TTV_Bezier2 * T1, TTV_Bezier2 * T2);

void supprimer_TTV_Bezier2(TTV_Bezier2 *ptr_T);

#endif

bezier.h:

#ifndef _BEZIER_H_
#define _BEZIER_H_

#include "TTV_Bezier2.h"
#include "TTV_Point.h"
#include "geom2d.h"

//Definition du type Bezier2
typedef struct Bezier_q
{
    Point C0;
    Point C1;
    Point C2;
}Bezier2;

//Definition du type Bezier3
typedef struct Bezier_s
{
    Point C0;
    Point C1;
    Point C2;
    Point C3;
}Bezier3;

//Fonction qui calcule le point de la Bezier quadratique C(t) au parametre t
Point calculate_bezier_2(Bezier2 b, double t);

//Fonction qui calcule le point de la Bezier cubique C(t) au parametre t
Point calculate_bezier_3(Bezier3 b, double t);

//Fonction qui calcule la distance entre un point et une bezier quadratique
double distance_point_bezier2(Point P, Bezier2 B, double t);

//Fonction qui calcule la distance entre un point et une bezier cubique
double distance_point_bezier3(Point P, Bezier3 B, double t);

//Fonction qui calcule le factoriel d'un nombre
double factorial (int i);

//Fonction qui prend une Bezier quadratique et la transforme en Bezier cubique
Bezier3 convert_bezier2_to_3(Bezier2 b);

//Fonction qui calcule la base bernstein B(i,d,t)
double calculate_bernstein_base(int i, int d, double t);

//Fonction qui calcule γ(k)
double calculate_gamma(int i, int n);

//Fonction qui approxime la sequence de points CONT par une Bezier quadratique
Bezier2 approx_bezier2(Point * tab, int j1, int j2);

//Fonction qui approxime la sequence de points CONT par une Bezier cubique
Bezier3 approx_bezier3(Point * tab, int j1, int j2);

//Fonction qui simplifie le contour avec une Bezier cubique
Bezier3_t * simplification_douglas_peucker_bezier3(Point * CONT, int j1, int j2, double d);

//Fonction qui simplifie le contour avec une Bezier quadratique
TTV_Bezier2 * simplification_douglas_peucker_bezier2(TTV_Point CONT, int j1, int j2, double d);

#endif /* _BEZIER_H_ */

test_simplification_bezier2.c:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "image_pbm.h"
#include "geom2d.h"
#include "TTV_Point.h"
#include "TTV_Bezier2.h"
#include "calcul_contours_multiples.h"

int main(int argc, char * argv[])
{
    if (argc != 4)
    {
        printf("Usage: ./test_simplification_bezier <source.pbm> <destination.eps> <distance_seuil>\n");
        return 0;
    }
    
    FILE * f;
    double distance_seuil;
    int i = 0;
    Image M, P;
    Point init;
    TTV_Point contour;
    TTV_Bezier2 * L;
    
    L = creer_TTV_Bezier2_vide();
    contour = creer_TTV_Point_vide();
    
    f = fopen(argv[2], "w");
    distance_seuil = atof(argv[3]);
    
    //Lecture de l'image
    M = lire_fichier_image(argv[1]);
    
    //Creation de l'image masque
    P = create_mask_image(M);
    
    fprintf(f, "%%!PS-Adobe-3.0 EPSF-3.0\n%%%%BoundingBox: 0 0 %d %d\n0 setlinewidth\n", M.L, M.H);
    while(is_empty(P) == -1)
    {
        init = trouver_point_init(P);
        contour = trouver_pixel_depart_TTV(M,P,init);
        while(i < nb_elements_TTV_Point(contour))
        {
            contour.tab[i].y = fabs((double) M.H - contour.tab[i].y);
            i++;
        }
        i = 0;
        L = simplification_douglas_peucker_bezier2(contour, 0, nb_elements_TTV_Point(contour) - 1, distance_seuil);
        write_to_file_bezier2(L,f);
    }
    fprintf(f, "\nfill\nshowpage");
    fclose(f);
    return 0;
}

【问题讨论】:

  • 循环包含?您的任何头文件是否包含(直接或间接)包含第一个头文件?
  • @Someprogrammerdude 每个文件都包含另一个文件。 bezier.h 包含 TTV_Bezier2.h,因为它需要类型 TTV_Bezier2 来定义函数 simplification_douglas_peucker2,而 TTV_Bezier2.h 包含 bezier.h,因为它需要类型 Bezier2 来定义类型 TTV_Bezier2 和函数 element_TTV_Bezier2。而且我不知道为什么这会是一个问题,因为在 bezier.h 我包含 TTV_Bezier.h 之后我定义了函数 simplification_douglas_peucker_bezier2。

标签: c compiler-errors compilation


【解决方案1】:

TTV_Bezier2.h 中包含bezier.h,在bezier.h 中包含TTV_Bezier2.h,这是问题的根源

但是在bezier.h中你不需要知道TTV_Bezier2的确切定义,因为你只是通过指针引用它,所以在bezier.h中:

  • 删除#include "TTV_Bezier2.h"
  • 添加struct TTV_Bezier2_;
  • simplification_douglas_peucker_bezier2的返回类型替换为struct TTV_Bezier2_ *

并在需要时在源中添加#include "TTV_Bezier2.h"

【讨论】:

    【解决方案2】:

    bezier.h 和 TTV_Bezier2.h 之间存在循环依赖关系

    如果您认为前身#include 函数只是将源代码扁平化为编译器的一个巨大文件,从 main.c 开始:

    • 主要包括(除其他外)TTV_Bezier.h
    • TTV_Bezier.h 在定义或声明 TTV_Bezier2 之前包含 bezier.h
    • bezier.h 将包含 TTV_Bezier.h,但由于包含保护,它不会产生任何影响(这是一件好事)

    如果您想象编译器内存中的扁平化预处理器输出,bezier.h 的内容在提及 TTV_Bezier 之前就存在,因此您会看到您看到的错误。

    要解决此问题,您可以:

    • TTV_Bezier2 * simplification_douglas_peucker_bezier2 原型移至 TTV_Bezier2.h
    • 或将两个标题所需的结构移动到新的第三个标题中,并将其包含在两个标题中。
    • 或者按照 bruno 的回答并转发声明结构

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多