鱼C论坛

 找回密码
 立即注册
查看: 1179|回复: 14

[已解决]DEV 头文件和源文件

[复制链接]
发表于 2023-7-5 19:04:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zhenjen 于 2023-7-5 19:40 编辑
  1. #include <limits.h>
  2. #include <stdlib.h>
  3. #include<stdio.h>

  4. #ifndef HEAP
  5. #define HEAP
  6. #define  Cap 20
  7. struct Heap;

  8. typedef struct Heap *H;
  9. H Initialize (H heap );
  10. int Isfull (H heap);
  11. int Isempty( H heap);
  12. void Insert (H heap ,int num);
  13. void DeleteMin(H heap);
  14. void printf_all (H heap);

  15. #endif
  16. struct Heap
  17. {
  18.         int Capacity; //数组容量
  19.         int Size;     //当前数量
  20.         int* List;
  21. };


  22. H  Initialize (H heap)
  23. {
  24.   if(!heap)
  25.   return heap;
  26.   heap =(H)malloc(sizeof(struct Heap));
  27.   if(!heap)
  28.   {
  29.     perror("malloc error") ;
  30.     exit(EXIT_FAILURE);
  31.   }
  32.    heap->List=(int*)malloc((Cap+1)*sizeof(int));
  33.    if(!heap->List)
  34.    {
  35.           perror("malloc error") ;
  36.       exit(EXIT_FAILURE);
  37.    }
  38.    else
  39.    {
  40.            heap->Capacity=Cap;
  41.            heap->Size=0;
  42.            heap->List[0]=INT_MIN;
  43.           
  44.    }
  45.    return heap;
  46.    
  47.   }
  48.   int Isempty (H heap)
  49. {
  50.         return !!(heap->Size==0);
  51.   }  
  52.   int Isfull(H heap)
  53.   {
  54.           return !!(heap->Capacity==Cap);
  55.   }
  56.   void Insert (H heap, int num)
  57.   {
  58.           int i;
  59.           if(!heap)
  60.           {
  61.       perror("heap is empty") ;
  62.       exit(EXIT_FAILURE);
  63.           }
  64.           for(i=heap->Size+1;heap->List[i/2]>num;i/=2)
  65.              heap->List[i]=heap->List[i/2];
  66.              heap->List[i]=num;
  67.          
  68.   }
  69.   void DeleteMin(H heap)
  70.   {
  71.           int i,child;
  72.           int Min,Max;
  73.           if(!heap)
  74.           {
  75.       perror("heap is empty") ;
  76.       exit(EXIT_FAILURE);
  77.           }
  78.           Min=heap->List[1];
  79.           Max=heap->List[heap->Size-1];
  80.           for(i=1;i*=2<=heap->Size;i=child)
  81.           {
  82.                   child=i*2;
  83.                   if(heap->Size>child&&heap->List[child]>heap->List[child+1])
  84.                   child++;
  85.                   if(Max>heap->List[child])
  86.                   heap->List[i]=heap->List[child];
  87.                   else
  88.                   break;
  89.           }
  90.           heap->List[i]=Max;
  91.   }
  92. void printf_all (H heap)
  93. {
  94.          for(int i=1;(heap->Size+1)>i;i++)
  95.           printf("1");
  96. }
  97. #include"Header.h "



  98. int main()
  99. {
  100.         struct Heap *heap=NULL;

  101.         heap=Initialize(heap);
  102.     int n=0;
  103.     while(scanf("%d",&n)==1)
  104.     {
  105.             Insert(heap,n);
  106.         }
  107.    printf_all(heap);
  108.        
  109. }



复制代码


[/code][/code][/code][/code]头文件进行函数声明;(一个头文件二个源文件)
其中一个源文件进行函数定义;(文件中有#include“头文件”)
问题在包含main()函数源文件报错:函数未定义(#include“头文件”)
头文件的代码
最佳答案
2023-7-5 19:44:07
[b]你的代码中主要有以下几个问题需要注意:

1.  #include"Header.h " 这行代码包含了一个不必要的空格,它可能阻止编译器正确找到头文件。你应该使用 #include "Header.h" 。

2. 你在定义 struct Heap 之前就已经开始使用了这个类型,这可能会导致编译器报告未定义类型的错误。你需要先定义类型,然后再使用。

3. 在你的 Insert 函数中,当 heap->List[i/2]>num 为 false 时,会跳出循环,但此时 i 已经被除以2,这可能会导致数组越界错误。你需要在分配值给 heap->List[i] 之前修复 i 。

4. 在 DeleteMin 函数中, heap->Size-1 会越过数组边界,如果 heap->Size 是0,那么结果是一个负数,这在C中是未定义的行为。

5. 你的代码中,heap的实例化在 Initialize 函数中发生,而你在main函数中传入的heap是 NULL ,这不是一个好的实践。你可以直接在 Initialize 函数中创建heap,无需在函数外部创建。

以下是修改后的代码:

Header.h
  1. #ifndef HEAP
  2. #define HEAP
  3. #define  Cap 20

  4. struct Heap {
  5.     int Capacity; //数组容量
  6.     int Size;     //当前数量
  7.     int* List;
  8. };

  9. typedef struct Heap *H;

  10. H Initialize();
  11. int Isfull(H heap);
  12. int Isempty(H heap);
  13. void Insert(H heap, int num);
  14. void DeleteMin(H heap);
  15. void printf_all(H heap);

  16. #endif
复制代码


Heap.c
  1. #include <limits.h>
  2. #include <stdlib.h>
  3. #include<stdio.h>
  4. #include "Header.h"

  5. H Initialize() {
  6.     H heap;
  7.     heap =(H)malloc(sizeof(struct Heap));
  8.     if(!heap) {
  9.         perror("malloc error");
  10.         exit(EXIT_FAILURE);
  11.     }
  12.     heap->List=(int*)malloc((Cap+1)*sizeof(int));
  13.     if(!heap->List) {
  14.         perror("malloc error") ;
  15.         exit(EXIT_FAILURE);
  16.     } else {
  17.         heap->Capacity=Cap;
  18.         heap->Size=0;
  19.         heap->List[0]=INT_MIN;
  20.     }
  21.     return heap;
  22. }

  23. int Isempty (H heap) {
  24.     return heap->Size == 0;
  25. }

  26. int Isfull(H heap) {
  27.     return heap->Size == Cap;
  28. }

  29. void Insert (H heap, int num) {
  30.     int i;
  31.     if(!heap) {
  32.         perror("heap is empty") ;
  33.         exit(EXIT_FAILURE);
  34.     }
  35.     for(i=heap->Size+1;heap->List[i/2]>num;i/=2)
  36.         heap->List[i]=heap->List[i/2];
  37.     heap->List[i/2]=num;
  38.     heap->Size++;
  39. }

  40. void DeleteMin(H heap) {
  41.     int i,child;
  42.     int Min,Max;
  43.     if(!heap) {
  44.         perror("heap is empty") ;
  45.         exit(EXIT_FAILURE);
  46.     }
  47.     Min=heap->List[1];
  48.     Max=heap->List[heap->Size];
  49.     for(i=1;i*2<=heap->Size;i=child) {
  50.         child=i*2;
  51.         if(child != heap·>Size && heap->List[child] > heap->List[child+1])
  52.             child++;
  53.         if(Max > heap->List[child])
  54.             heap->List[i]=heap->List[child];
  55.         else
  56.             break;
  57.     }
  58.     heap->List[i]=Max;
  59.     heap->Size--;
  60. }

  61. void printf_all(H heap) {
  62.     for(int i=1;i<=heap->Size;i++)
  63.         printf("%d\n", heap->List[i]);
  64. }
复制代码


main.c
  1. #include <stdio.h>
  2. #include "Header.h"

  3. int main() {
  4.     H heap = Initialize();
  5.     int n=0;
  6.     while(scanf("%d",&n)==1) {
  7.         Insert(heap,n);
  8.     }
  9.     printf_all(heap);
  10.     return 0;
  11. }
复制代码


注意,这些代码不会实现一个有效的堆数据结构,因为 Insert 和 DeleteMin 函数的实现不正确,它们不会保持堆的属性。你可能需要重新实现这两个函数以实现有效的堆数据结构。[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 19:09:22 | 显示全部楼层
函数返回值类型一致吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-5 19:11:07 | 显示全部楼层
where's your code?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 19:12:48 | 显示全部楼层
豆嘉木 发表于 2023-7-5 19:09
函数返回值类型一致吗
zhizhi

一致
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 19:13:56 | 显示全部楼层
报错:E:\c编程练习\数据算法\main.o        In function `main':
18                E:\c编程练习\数据算法\main.c        undefined reference to `Initialize'
22                E:\c编程练习\数据算法\main.c        undefined reference to `Insert'
24                E:\c编程练习\数据算法\main.c        undefined reference to `printf_all'
E:\c编程练习\数据算法\collect2.exe        [Error] ld returned 1 exit status
26                E:\c编程练习\数据算法\Makefile.win        recipe for target '6.2.exe' failed
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 19:19:18 | 显示全部楼层

发了发了

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-5 19:23:16 | 显示全部楼层

发在代码标签里

                               
登录/注册后可看大图

不然会冲突
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 19:40:41 | 显示全部楼层
歌者文明清理员 发表于 2023-7-5 19:23
发在代码标签里

不然会冲突

好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-5 19:42:16 | 显示全部楼层

头文件呢limits.h
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-5 19:44:07 | 显示全部楼层    本楼为最佳答案   
[b]你的代码中主要有以下几个问题需要注意:

1.  #include"Header.h " 这行代码包含了一个不必要的空格,它可能阻止编译器正确找到头文件。你应该使用 #include "Header.h" 。

2. 你在定义 struct Heap 之前就已经开始使用了这个类型,这可能会导致编译器报告未定义类型的错误。你需要先定义类型,然后再使用。

3. 在你的 Insert 函数中,当 heap->List[i/2]>num 为 false 时,会跳出循环,但此时 i 已经被除以2,这可能会导致数组越界错误。你需要在分配值给 heap->List[i] 之前修复 i 。

4. 在 DeleteMin 函数中, heap->Size-1 会越过数组边界,如果 heap->Size 是0,那么结果是一个负数,这在C中是未定义的行为。

5. 你的代码中,heap的实例化在 Initialize 函数中发生,而你在main函数中传入的heap是 NULL ,这不是一个好的实践。你可以直接在 Initialize 函数中创建heap,无需在函数外部创建。

以下是修改后的代码:

Header.h
  1. #ifndef HEAP
  2. #define HEAP
  3. #define  Cap 20

  4. struct Heap {
  5.     int Capacity; //数组容量
  6.     int Size;     //当前数量
  7.     int* List;
  8. };

  9. typedef struct Heap *H;

  10. H Initialize();
  11. int Isfull(H heap);
  12. int Isempty(H heap);
  13. void Insert(H heap, int num);
  14. void DeleteMin(H heap);
  15. void printf_all(H heap);

  16. #endif
复制代码


Heap.c
  1. #include <limits.h>
  2. #include <stdlib.h>
  3. #include<stdio.h>
  4. #include "Header.h"

  5. H Initialize() {
  6.     H heap;
  7.     heap =(H)malloc(sizeof(struct Heap));
  8.     if(!heap) {
  9.         perror("malloc error");
  10.         exit(EXIT_FAILURE);
  11.     }
  12.     heap->List=(int*)malloc((Cap+1)*sizeof(int));
  13.     if(!heap->List) {
  14.         perror("malloc error") ;
  15.         exit(EXIT_FAILURE);
  16.     } else {
  17.         heap->Capacity=Cap;
  18.         heap->Size=0;
  19.         heap->List[0]=INT_MIN;
  20.     }
  21.     return heap;
  22. }

  23. int Isempty (H heap) {
  24.     return heap->Size == 0;
  25. }

  26. int Isfull(H heap) {
  27.     return heap->Size == Cap;
  28. }

  29. void Insert (H heap, int num) {
  30.     int i;
  31.     if(!heap) {
  32.         perror("heap is empty") ;
  33.         exit(EXIT_FAILURE);
  34.     }
  35.     for(i=heap->Size+1;heap->List[i/2]>num;i/=2)
  36.         heap->List[i]=heap->List[i/2];
  37.     heap->List[i/2]=num;
  38.     heap->Size++;
  39. }

  40. void DeleteMin(H heap) {
  41.     int i,child;
  42.     int Min,Max;
  43.     if(!heap) {
  44.         perror("heap is empty") ;
  45.         exit(EXIT_FAILURE);
  46.     }
  47.     Min=heap->List[1];
  48.     Max=heap->List[heap->Size];
  49.     for(i=1;i*2<=heap->Size;i=child) {
  50.         child=i*2;
  51.         if(child != heap·>Size && heap->List[child] > heap->List[child+1])
  52.             child++;
  53.         if(Max > heap->List[child])
  54.             heap->List[i]=heap->List[child];
  55.         else
  56.             break;
  57.     }
  58.     heap->List[i]=Max;
  59.     heap->Size--;
  60. }

  61. void printf_all(H heap) {
  62.     for(int i=1;i<=heap->Size;i++)
  63.         printf("%d\n", heap->List[i]);
  64. }
复制代码


main.c
  1. #include <stdio.h>
  2. #include "Header.h"

  3. int main() {
  4.     H heap = Initialize();
  5.     int n=0;
  6.     while(scanf("%d",&n)==1) {
  7.         Insert(heap,n);
  8.     }
  9.     printf_all(heap);
  10.     return 0;
  11. }
复制代码


注意,这些代码不会实现一个有效的堆数据结构,因为 Insert 和 DeleteMin 函数的实现不正确,它们不会保持堆的属性。你可能需要重新实现这两个函数以实现有效的堆数据结构。[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 19:50:05 From FishC Mobile | 显示全部楼层
歌者文明清理员 发表于 2023-7-5 19:42
头文件呢limits.h

第一个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 19:50:27 From FishC Mobile | 显示全部楼层
zhenjen 发表于 2023-7-5 19:50
第一个

有函数声明的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 19:53:25 From FishC Mobile | 显示全部楼层
zhenjen 发表于 2023-7-5 19:50
有函数声明的

源文件是定义函数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-5 20:34:06 | 显示全部楼层
本帖最后由 zhenjen 于 2023-7-5 21:10 编辑

1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 20:38:55 | 显示全部楼层
isdkz 发表于 2023-7-5 19:44
你的代码中主要有以下几个问题需要注意:

1.  #include"Header.h " 这行代码包含了一个不必要的空格,它 ...

为什么会不识别定义了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 15:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表