在xcode中构建错误
Error building in xcode
我有一个main函数,看起来像这样:
#include <vector>
#include "mins_ndim.h"
#include "ObjectiveFunction.h"
int main (int argc, char * const argv[]) {
ObjectiveFunction objective;
Frprmn<ObjectiveFunction> frprmn(objective);
std::vector<double> p(2);
p[0]=7; p[1]=3;
frprmn.eat();
}
但是这给了我错误:
Undefined symbols:
"Frprmn<ObjectiveFunction>::eat()", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
eat()
列在mins_ndim.h
中Frprmn的结构定义中,在mins_ndim.cpp
中定义。如果我在mins_ndim.h
中定义它,那么我就不会得到这个错误。我认为这是与链接有关,但我不知道如何得到xcode排序?!
类模板的成员函数的定义在使用时必须对编译器可见。当编译器遇到frprmn.eat();
时,它没有看到eat
的实现(它只看到了mins_ndim.h
中的声明),因此不能生成适当的代码。
请记住,模板本质上是一段代码,编译器必须将其实例化,并将模板类型替换为适当的模板参数。编译器需要能够看到eat
的实现,以便能够以ObjectiveFunction
作为模板实参来生成它。
因此,在头文件中实现函数模板或类模板的成员函数是一种常见的做法。
如果你想保持类模板的定义和它的实现是分开的,另一种方法是在头文件的底部包含实现文件。这与典型的包含方法相反。为了更清楚地说明这一点,通常使用.tpp
扩展名(t
表示模板)来命名实现文件。
相关文章:
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- Clion显示错误,但可以使用Cmake成功构建代码
- Agora.io 虚幻引擎插件构建错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 如何构建 SOCI 库而没有错误?
- 尝试将 boost::stacktrace 添加到 CMake 项目时出现构建错误
- Arch Linux.AUR 包 mysql 不能用 makepkg 构建.错误:构建 () 中出现故障
- GCC:随机构建导致执行期间分段错误
- 组件上的虚幻引擎可蓝图UFUNCTION会导致构建错误
- 构建失败错误:无法使用 qibuild 构建C++ SDK Helloworld
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- MINGW的错误构建提升
- 从Canfestival错误构建CanOpen的示例
- 链接器错误构建GDAL VS2017
- Netbeans 7.4 C++简单的 hello world 错误构建失败尝试了 Google 搜索结果前 2 页上的
- 链接器错误构建 fibonnacci 示例与 boost::coroutine2 与 Boost 1.60 使用动态链接
- 使用CMake错误构建Thor SFML扩展
- 链接器错误构建GDAL
- 错误构建简单的MongoDB c++客户端示例