将struct传递给函数会得到“未定义的引用”错误

将struct传递给函数会得到“未定义的引用”错误,c,struct,C,Struct,我在与C的练习中遇到了一个问题,希望stack overflow的人能帮我解决这个问题。背后的故事是,我想对如何使用结构有一个坚实的理解,所以我开始使用这个概念,但当我开始使用数组并开始将结构传递给函数时,遇到了一个问题 在Linux中使用G++编译代码时,出现编译错误: /tmp/ccCjoSgv.o: In function `main': main.c:(.text+0x10e): undefined reference to `indexBooks(int, book)' collect

我在与C的练习中遇到了一个问题,希望stack overflow的人能帮我解决这个问题。背后的故事是,我想对如何使用结构有一个坚实的理解,所以我开始使用这个概念,但当我开始使用数组并开始将结构传递给函数时,遇到了一个问题

在Linux中使用G++编译代码时,出现编译错误:

/tmp/ccCjoSgv.o: In function `main':
main.c:(.text+0x10e): undefined reference to `indexBooks(int, book)'
collect2: error: ld returned 1 exit status
我花了几个小时试图自己解决类似的堆栈溢出问题,但仍然无法理解为什么会出现这个编译错误。有人能提供他们的专业知识并解释我为什么会出现这些错误吗

主要包括

内干管

主函数调用

实际功能


您的功能原型和实现不匹配:

void indexBooks(int usrNum, struct book science);
void indexBooks(int usrNum, struct book science[]){
原型:

void indexBooks(int, struct book science);  // science should be an array instead
实施:

void indexBooks(int usrNum, struct book science[])
看起来您的原型是错误的,indexBooks采用的是结构数组而不是单个结构

您应该首先尝试修复原型:

void indexBooks(int, struct book science[]);
同样在main中,您应该传递整个数组,而不是它的第一个元素:

indexBooks(usrNum, *science); //record the books using struct // incorrect
应该是

indexBooks(usrNum, science);

您声明的原型与您的函数定义不匹配。你宣布:

void indexBooks(int, struct book science);
但你在定义:


请注意定义中的括号。您的声明声明了一个函数,该函数只接受一个struct book参数,但定义接受一个struct book数组。您应该将声明更改为void indexBooksint usrNum,struct book science[]。原型和函数不匹配:

void indexBooks(int usrNum, struct book science);
void indexBooks(int usrNum, struct book science[]){

一个是数组一个是结构….

原型:void indexBooksint,struct book science;实现:ValueRexBoogleSux.UrnNUM,Stutt BoooSo[科学] []见差异?G++是C++编译器,而不是C编译器。那么,应该是C还是C++代码?他们是不同的语言。删除不相关的标签并编辑文本!哇那太傻了。。。非常感谢。您似乎仍然在使用g++!G+是我学习C++时养成的习惯。我将使用gcc。谢谢你指出这一点,顺便说一句:这两个都是正确的,这取决于你打算做什么,因为函数引用了每个数组地址,第二个数组地址用[]表示正确,请更新原型以包含方括号。没问题。这里的措词有点离题:指向数组的指针OP没有发送指针,而是取消了对指针的引用并意外地发送了第一个元素的副本。或者,如果它被编译的话,它们会是。
void indexBooks(int, struct book science[]);
indexBooks(usrNum, *science); //record the books using struct // incorrect
indexBooks(usrNum, science);
void indexBooks(int, struct book science);
void indexBooks(int usrNum, struct book science[])
void indexBooks(int usrNum, struct book science);
void indexBooks(int usrNum, struct book science[]){