dev C++ 未定义的引用
dev C++ Undefined references?
所以这是我程序的一部分,我在调用函数时遇到问题,我真的需要一些帮助。基本上是选择任一功能并输入数据和 稍后打印该数据。 做错了什么?请帮忙,我一直得到
"[Linker] 对
Customer_Record()'" , [Linker error] undefined reference to
Car_Record()
' 和"ld 返回 1 个退出状态"的引用"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
void Customer_Record(), Car_Record();
int num;
struct Customer {
char customer_ID[20];
int license;
char address[20];
int phone;
char email[20];
} cust;
struct car {
int regno[20];
char model[20];
char colour[10];
} car;
main() {
printf(" Enter 1 to go to Customer Record nn");
printf(" Enter 2 to go to Car Record nn");
scanf("%d", &num);
if (num = 1) {
Customer_Record();
} else if (num = 2) {
Car_Record();
} else {
printf("Invalid Number");
}
system("cls");
void Customer_Record(); {
printf("********CUSTOMER RECORD********"); /* accepts into*/
printf("nEnter the name of the customer ");
scanf("%s", &cust.customer_ID);
printf("Enter the license number of the customer ");
scanf("%d", &cust.license);
printf("Enter the address of the customer ");
scanf("%s", &cust.address);
printf("Enter the cell phone number of the customer ");
scanf("%d", &cust.phone);
printf("Enter the email address of the customer ");
scanf("%s", &cust.email);
}
void Car_Record(); {
printf("********CAR RECORD********");
printf("nEnter the car's registration number ");
scanf("%d", &car.regno);
printf("Enter the car's model ");
scanf("%s", &car.model);
printf("Enter the colour of the car ");
scanf("%s", &car.colour);
}
getchar();
getchar();
}
不要像那样嵌套你的函数。 Customer_Record()
和Car_Record()
的定义应该在main()
之外。 您还需要从这些函数的定义中解脱;
。
尝试更好地格式化代码 - 从长远来看,这将有很大帮助。
-
你在 main 的末尾缺少一个 },编译器认为你的函数声明在主函数内。
-
从函数中删除尾随分号。前任:
void Car_Record(); {
自
void Car_Record() {
该分号不是必需的。
我已经编制了一个程序所有问题的列表。在这里:
-
if 语句使用赋值
=
运算符,而它们应该使用比较运算符==
。 例如,更改以下内容:if (num = 1) {
自
if (num == 1) {
这也出现在您的
else
语句中。 -
在 main 内部定义函数也是不正确的。必须在主子句之外定义这些块。您已经在 main 上方对函数进行了原型设计,现在您必须在 main 下面定义它们。此外,定义函数时,参数列表后不应有分号;这在语法上是错误的。
以下是建议。您正在编译此代码C++但这是使用 C 函数/标头编写的。要将其转换为C++,请执行以下更改:
-
更改你的标题:stdio.h,conio.h,stdlib.h;这些都是C样式的标题。基本上,所有以".h"结尾的标头都是 C 样式的标头。C++有自己的 I/O 库,因此使其 C 等效项过时。请改为包含以下标头:
#include <iostream> #include <cstdlib>
我省略了额外的标头,因为您似乎只使用
printf
/scanf
和system
,相当于 C++ 的iostream
和cstdlib
标头已经拥有的标头。例如,std::cout
和std::cin
for iosteam。相当于getchar
是std::cin.get()'。 -
主返回 int:在标准C++中,不能省略返回类型。将 main 的返回类型指定为
int
,但不必将return 0
放在末尾(这是隐式的)。
如果要查找C++函数和容器(如std::cout
/std::cin
),此参考有很大帮助。
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中