在xcode中构建错误

Error building in xcode

本文关键字:错误 构建 xcode      更新时间:2023-10-16

我有一个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表示模板)来命名实现文件。