致命错误LNK1120:1个未解析的外部
fatal error LNK1120: 1 unresolved externals
我正在编译以下代码,收到一个错误。我想在boost中练习模板,我不知道如何处理这个问题
#include <stdafx.h>
#include <iostream>
#include <string>
#include <boost/function.hpp>
#include <boost/array.hpp>
using namespace std;
template<typename R,typename D>
class GenericFunction
{
private:
boost::function<R (D)> f;
protected:
GenericFunction();
public:
GenericFunction(const boost::function<R (D)>& myFunction);
R evaluate(const D& value) const ;
R operator ()(const D& value) const;
};
template <typename R, typename D, int N>
class ScalarValuedFunction:public GenericFunction<R,boost::array<D, N>>
{
public:
ScalarValuedFunction(const boost::function<R (const boost::array<D, N>)> &myF);
};
template<typename Numeric, std::size_t N>
Numeric Norm(const boost::array<Numeric , N>& Vec)
{
Numeric Result=Vec[0]*Vec[0];
for (std::size_t i=1; i<Vec.size();i++)
{
Result+=Vec[i]*Vec[i];
}
return Result;
}
int main ()
{
const int N=4;
boost::array<double, N> arr={0.2,.3,1.1,4};
ScalarValuedFunction<double, double, N> myfun(Norm<double,N>);
}
并且接收到以下错误,
ConsoleApplication2.obj : error LNK2019: unresolved external symbol "public: __thiscall ScalarValuedFunction<double,double,4>::ScalarValuedFunction<double,double,4>(class boost::function<double __cdecl(class boost::array<double,4>)> const &)" (??0?$ScalarValuedFunction@NN$03@@QAE@ABV?$function@$$A6ANV?$array@N$03@boost@@@Z@boost@@@Z) referenced in function _main
1>c:usersuniversitydocumentsvisual studio 2012ProjectsConsoleApplication2DebugConsoleApplication2.exe : fatal error LNK1120: 1 unresolved externals
请有人告诉我我的代码出了什么问题?
您没有为ScalarValuedFunction
类模板的构造函数提供定义,但在类模板的定义中声明了:
ScalarValuedFunction(const boost::function<R (const boost::array<D, N>)> &myF);
因此,链接器抱怨无法解析对构造函数的调用。呼叫发生在main()
:中
ScalarValuedFunction<double, double, N> myfun(Norm<double,N>);
若要解决您的问题,请为构造函数添加一个定义。例如:
template<typename R, typename D, int N>
ScalarValuedFunction<R, D, N:::ScalarValuedFunction(
const boost::function<R (const boost::array<D, N>)> &myF
)
:
GenericFunction<R, boost::array<D, N>>::GenericFunction(myMF)
{ }
您还没有为ScalarValuedFunction
类定义构造函数,只是声明了它。
相关文章:
- C++、错误LNK1120和LNK2001中未解析的外部符号
- Visual Studio 2010 - C++ 致命错误 LNK1120: 1 个未解析的外部
- LNK2019和LNK1120错误,未解析的外部和未解析的符号
- 错误LNK1120:未解决的外部
- 如何在我的指针和阵列功能中修复未解决的外部错误-LNK2019和LNK1120
- C++ 错误 LNK1120:在静态函数内部调用时有 1 个未解析的外部
- c++中的静态构造函数和致命错误LNK1120:1个未解析的外部
- SDL错误LNK1120:1未解决的外部
- QT错误:LNK1120:1未解决的外部Main.OBJ:-1:错误:LNK2019运行QMAKE
- Qt LNK1120未解析的外部
- LNK1120:Visual Studio 2012 中 1 个未解析的外部
- 编译任何内容时出现奇怪的错误,VS 2010:致命错误LNK1120:1个未解决的外部
- 错误 LNK2019:未解析的外部和LNK1120:1 个未解析的外部
- 操作员过载未解决的外部错误LNK1120、LNK2019
- visual C++LNK1120致命错误:1个未解析的外部
- 致命错误LNK1120:1 个未解析的外部 & 错误 LNK2019:函数___tmainCRTStartup中引用_main未解析的外部符号
- Ogre 3D错误-LNK1120:1个未解析的外部
- 致命错误LNK1120:8个未解析的外部
- LNK2019:函数"函数"中'symbol'引用未解析的外部符号;致命错误 LNK1120:1 未解析的外部
- c++静态变量在Visual Studio中产生致命错误LNK1120: 1未解析的外部