首页 新闻 会员 周边

dev 头文件与源文件的问题

0
悬赏园豆:20 [待解决问题]

头文件进行函数声明;(一个头文件二个源文件)
其中一个源文件进行函数定义;(文件中有#include“头文件”)
问题在包含main()函数源文件报错:函数未定义(#include“头文件”)

问题补充:

头文件的代码

include <limits.h>

include <stdlib.h>

include<stdio.h>

ifndef HEAP

define HEAP

define Cap 20

struct Heap;

typedef struct Heap *H;
H Initialize (H heap );
int Isfull (H heap);
int Isempty( H heap);
void Insert (H heap ,int num);
void DeleteMin(H heap);
void printf_all (H heap);

endif

struct Heap
{
int Capacity; //数组容量
int Size; //当前数量
int* List;
};

定义函数的源文件代码

H Initialize (H heap)
{
if(!heap)
return heap;
heap =(H)malloc(sizeof(struct Heap));
if(!heap)
{
perror("malloc error") ;
exit(EXIT_FAILURE);
}
heap->List=(int)malloc((Cap+1)sizeof(int));
if(!heap->List)
{
perror("malloc error") ;
exit(EXIT_FAILURE);
}
else
{
heap->Capacity=Cap;
heap->Size=0;
heap->List[0]=INT_MIN;

}
return heap;

}
int Isempty (H heap)
{
return !!(heap->Size0);
}
int Isfull(H heap)
{
return !!(heap->Capacity
Cap);
}
void Insert (H heap, int num)
{
int i;
if(!heap)
{
perror("heap is empty") ;
exit(EXIT_FAILURE);
}
for(i=heap->Size+1;heap->List[i/2]>num;i/=2)
heap->List[i]=heap->List[i/2];
heap->List[i]=num;

}
void DeleteMin(H heap)
{
int i,child;
int Min,Max;
if(!heap)
{
perror("heap is empty") ;
exit(EXIT_FAILURE);
}
Min=heap->List[1];
Max=heap->List[heap->Size-1];
for(i=1;i=2<=heap->Size;i=child)
{
child=i
2;
if(heap->Size>child&&heap->List[child]>heap->List[child+1])
child++;
if(Max>heap->List[child])
heap->List[i]=heap->List[child];
else
break;
}
heap->List[i]=Max;
}
void printf_all (H heap)
{
for(int i=1;(heap->Size+1)>i;i++)
printf("1");
}
包含main()源文件代码

include"Header.h "

int main()
{
struct Heap *heap=NULL;

heap=Initialize(heap); 
int n=0;
while(scanf("%d",&n)==1)
{
	Insert(heap,n);
}

printf_all(heap);

}

mintierr的主页 mintierr | 初学一级 | 园豆:184
提问于:2023-07-05 18:34
< >
分享
所有回答(3)
0

函数名字写错了? 把两个文件的内容放在同一个文件里编译会报错吗

jakio6 | 园豆:1318 (小虾三级) | 2023-07-05 18:39

undefined reference to‘ 函数名字 ’

支持(0) 反对(0) mintierr | 园豆:184 (初学一级) | 2023-07-05 18:58
0

dev本身问题,重新创建一个新项目没有问题了。

mintierr | 园豆:184 (初学一级) | 2023-07-05 20:52
0

根据您提供的代码,出现函数未定义的错误可能是由于以下原因导致的:

头文件的条件编译问题:在头文件中,您使用了条件编译指令来避免头文件的重复包含。确保在头文件的开头和结尾都有相应的条件编译指令。在代码示例中,缺少了#endif来结束条件编译的部分。请确保头文件的最后加上#endif,并且条件编译的宏名与头文件的宏名一致。

头文件的重复包含问题:确认头文件在源文件中只包含一次。如果在源文件中多次包含同一个头文件,会导致函数重定义的错误。请检查您的源文件,确保Header.h只被包含一次。

头文件的路径问题:确保头文件的路径是正确的,并且与源文件在同一目录下或者使用正确的相对路径。如果头文件的路径不正确,编译器将无法找到头文件中的函数声明,导致函数未定义的错误。

请仔细检查上述问题,并进行相应的修正。如果问题仍然存在,请提供更详细的错误信息或代码示例,以便更好地帮助您解决问题。

Technologyforgood | 园豆:5718 (大侠五级) | 2023-07-06 22:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册