当我想运行代码时,编译器说 undefined reference to math::calc
我在 StackOverflow 上阅读了有关此问题的问答,但它并不能帮助我解决问题。
Comp.h
#include <utility>
namespace math {
typedef std::pair<double,double> Comp;
double getFirst(Comp a);
...
}
压缩文件#include "comp.h"
namespace math {
double getFirst(Comp a) {
return a.first;
}
...
}
Comp
文件:每个函数返回 Comp
或 double
.我从 cal.cpp
调用函数多次归档校准时间
#include "comp.h"
#include "helper.h"
namespace math {
Comp calc(const string& str);
...
}
调用程序#include "eval.h"
namespace math {
Comp calc(const string& str) {
...
}
}
Cal
文件:一些函数返回 comp
类型,而不仅仅是 cal
功能。helper.h
namespace math {
...
}
helper.cpp#include "helper.h"
namespace math {
...
}
helper
文件只包含我从 cal.cpp
调用的几个函数文件。每个函数都会调用多次。主程序
#include "calc.h"
int main() {
string str = " ";
pair<double,double> res = math::calc(str);
cout << res.first << " " << res.second << endl;
return 0;
}
在整个项目中,我没有使用任何类。我包含了我调用的每个文件,除了 c++ 原始文件。
我在每个文件中使用 std 命名空间,但我没有在这里写。
我完全不知道我的代码可能有什么问题。
如果我还包括
cal.cpp
在 main.cpp
代码编辑器说 undefined reference
到我从 helper.h
调用的每个文件.我不想包括 cal.cpp
在 main
我刚刚提到的文件
最佳答案
您必须编译所有项目的 *.cpp 文件,然后正确链接它们(编译结果) - 链接过程取决于您使用的环境/IDE,只需查找即可。如果您没有正确链接它,最终的可执行文件将不会具有所需的所有函数定义,因此会获得 undefined reference 。
关于c++ - 使用命名空间 C++ 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65813516/