过载链接错误

Overload linking error

本文关键字:错误 链接      更新时间:2023-10-16

所以我有一个带有默认值函数的标题:

head.h:

a(int x, int y, bool* b = NULL);

head.cpp:

a(int x, int y, bool* b = NULL){...}

source.cpp:

a(1, 2, NULL);
a(1, 2);

,我在链接阶段遇到了一个错误:

undefined reference to a(int, int, bool*)

我也尝试删除默认值:

head.h:

a(int x, int y, bool* b);
a(int x, int y);

head.cpp:

a(int x, int y, bool* b){...}
a(int x, int y){...}

source.cpp:

a(1, 2, NULL);
a(1, 2);

与相同错误链接:

undefined reference to a(int, int, bool*)
undefined reference to a(int, int)

这会是什么?我的编译器不好?不好的makefile?

编辑

它接缝了真实的标头文件,就像:

a(const int x, const int y, bool* b = NULL);

使用const,对不起。

它接缝我是在使用const变量的标题中定义函数,但在没有const的情况下在CPP中实现它,因此链接器未能找到函数的实现。

<</p> <</p>

gcc -o source source.cpp header.cpp命令应该像这样

我想你忘了header.cpp

尝试像这样

首先分别编译程序并生成对象文件

gcc -c header.c
gcc -c source.c

然后像这样生成exe

gcc header.o source.o -o exefile
相关文章: