首页 > 解决方案 > 为什么 std::string 和 std::vector 没有按照我的预期传递给我的构造函数?

问题描述

这是我尝试编译时的错误:

Undefined symbols for architecture x86_64:
    "Model::Model(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::vector<int, std::__1::allocator<int> >)", referenced from: ____C_A_T_C_H____T_E_S_T____4() in tests.cpp.o

我读过 std::string 是 std::basic_string 的 typedef ,我猜 std::vector 是... std::vector 和 std::allocator 的 typedef?或者其他的东西...

反正。我有一个包含我的设置的设置类和一个包含我的模型的模型类。我试图运行的代码如下,我无法编译它。问题在于它无法识别我的模型构造函数。

Settings s = Settings("../config/settings.json");
std::string mf = s.get_model_file();
std::vector<int> vec = s.get_input_shape();
Model m = Model(mf, vec);

这里是我的模型类头供参考:

class Model {
public:
  Model(std::string model_file, std::vector<int> input_shape);
  ~Model();
  double* get_ref_to_input_buffer();
  std::string predict();

private:
  std::string _model_file;
  fdeep::model _model;

  fdeep::shape3 _input_shape;
  int _input_size;
  double* _input_buffer;
  fdeep::tensor3s _result;

  void _load_model();
  void _set_input_size(std::vector<int> input_shape);
  void _set_input_shape(std::vector<int> input_shape);
  void _create_input_buffer();
  std::string _result_to_string();
};

和我的模型类构造函数:

Model::Model(std::string model_file, std::vector<int> input_shape) {
      _model_file = model_file;
      _load_model();
      _set_input_size(input_shape);
      _set_input_shape(input_shape);
}

这些是在构造函数中调用的函数:

void Model::_load_model() { _model = fdeep::load_model(_model_file); }

void Model::_set_input_size(std::vector<int> input_shape) {
  int total = 1;
  for (std::vector<int>::iterator it = input_shape.begin();
      it != input_shape.end(); ++it) {
    total *= *it;
  }
  _input_size = total;
}

void Model::_set_input_shape(std::vector<int> input_shape) {
  _input_shape = fdeep::shape3(input_shape[0], input_shape[1], input_shape[2]);
}

如果有人能指出我哪里出错了,或者把我送到我需要阅读/学习的方向,那就太好了。谢谢!

标签: c++classc++11constructor

解决方案


根据http://www.cplusplus.com/forum/general/55587/

“未定义的符号”通常表示链接问题。您确定正确链接到适当的库吗?

你的代码编译得很好。然后,链接器会在库中查找正确的函数,并且由于您针对架构 x86_64 进行了编译,因此它会查找正确的库,类似地编译 - 但没有找到。这表明(除非您只是忘记链接到 64 位库)您拥有该库的 32 位版本。您的选择是:

  1. 为 32 位构建代码,以便您可以使用已有的库。
  2. 获取库源代码,自己编译为x86_64
  3. 去寻找你找到库的任何地方,并希望在你上次找到的 32 位版本旁边有一个 x86_64 版本。

推荐阅读