【发布时间】:2020-01-23 10:21:37
【问题描述】:
我试图搜索类似的问题,但没有找到可以帮助我解决问题的线程。如果我错过了,请提前道歉!
即使我有使用其他语言(如 Java)的经验并且我正在使用 Visual Studio Express 2015,我对 C++ 还是很陌生。 所以,我有一个执行一些集合计算的类。使用输出迭代器的想法来自SO topic,我从中复制了代码的结构。
标题如下所示:
#pragma once
#include <tuple>
#include <vector>
#include <iterator>
using namespace std;
class SomeComputation
{
public:
SomeComputation(void);
~SomeComputation(void);
template <typename Range1, typename Range2, typename Range3, typename OutputIterator>
void compute(Range1 const & r1, Range2 const &r2, Range3 const & r3, OutputIterator out);
};
实现如下所示:
#include "SomeComputation.h"
using namespace std;
SomeComputation::SomeComputation(void)
{
// build the object
}
SomeComputation::~SomeComputation(void)
{
// destroy it
}
template <typename Range1, typename Range2, typename Range3, typename OutputIterator>
void SomeComputation::compute(Range1 const & r1, Range2 const &r2, Range3 const & r3, OutputIterator out) {
// do some set computation here
// then save the result using the output iterator
*out++ = std::make_tuple([OMISSIS]);
}
现在,该类被打包在一个静态库中,该库正确链接到另一个项目,我这样称呼它:
#include <vector>
#include <tuple>
#include <iterator>
#include <SomeComputation.h>
using namespace std;
void MyClass::myFunction(){
// init data (please assume that
//minQ1, maxQ1, etc. are double vars correctly initialized)
vector<double> rQ0q1;
rQ0q1.push_back(minQ1);
rQ0q1.push_back(maxQ1);
vector<double> rQ0q2;
rQ0q2.push_back(minQ2);
rQ0q2.push_back(maxQ2);
vector<double> rQ0r3;
rQ0r3.push_back(minR3);
rQ0r3.push_back(maxR3);
SomeComputation s = SomeComputation();
vector<tuple<double, double, double>> result;
s.compute(rQ0q1, rQ0q2, rQ0r3, std::back_inserter(result));
}
但是,我收到以下链接器错误 LNK2019:
Error LNK2019 unresolved external symbol "public: void __thiscall SomeComputation::compute<class std::vector<double,class std::allocator<double> >,class std::vector<double,class std::allocator<double> >,class std::vector<double,class std::allocator<double> >,class std::back_insert_iterator<class std::vector<class std::tuple<double,double,double>,class std::allocator<class std::tuple<double,double,double> > > > >(class std::vector<double,class std::allocator<double> > const &,class std::vector<double,class std::allocator<double> > const &,class std::vector<double,class std::allocator<double> > const &,class std::back_insert_iterator<class std::vector<class std::tuple<double,double,double>,class std::allocator<class std::tuple<double,double,double> > > >)" (??$compute@V?$vector@NV?$allocator@N@std@@@std@@V12@V12@V?$back_insert_iterator@V?$vector@V?$tuple@NNN@std@@V?$allocator@V?$tuple@NNN@std@@@2@@std@@@2@@SomeComputation@@QAEXABV?$vector@NV?$allocator@N@std@@@std@@00V?$back_insert_iterator@V?$vector@V?$tuple@NNN@std@@V?$allocator@V?$tuple@NNN@std@@@2@@std@@@2@@Z) referenced in function "public: void __thiscall ModelData::addDataAtFrame(int,class Vector,class Vector)" (?addDataAtFrame@ModelData@@QAEXHVVector@@0@Z) [path and project name omitted] 1
现在,虽然我可以通过使用“更传统”的返回值来解决错误,但我不明白是什么导致了它以及为什么会发生。我说该库已正确链接,因为我可以毫无问题地使用该库的所有其他函数/对象/类,但如果我使用这个,我会收到错误。谁能帮我澄清一下?
非常感谢。
【问题讨论】:
-
在这一行中:
s.compute(rQ0q1, rQ0q2, rQ0r3, std::back_inserter(result);最后缺少一个),但这可能来自复制 -
是的,这是复制时出现的错误。感谢您指出这一点,我将编辑问题。