致命错误LNK1120:1未解析外部 [英] fatal error LNK1120: 1 unresolved externals

查看:268
本文介绍了致命错误LNK1120:1未解析外部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编译以下代码,我收到一个错误。我想在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>
数值范数(const boost :: array< Numeric,N& Vec)
{
数值结果= 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:未解析的外部符号public:__thiscall ScalarValuedFunction< double,double,4> :: ScalarValuedFunction< double,double,4& :function 中引用1> c:\users\university \documents \visual studio 2012 \Projects\ConsoleApplication2\Debug \ConsoleApplication2.exe:致命错误LNK1120:1未解决的外部

请有人告诉我我的代码?

解决方案

您未提供 ScalarValuedFunction 类模板的构造函数,但仍然在类模板的定义中已声明:

  ScalarValuedFunction (const boost :: function< R(const boost :: array< D,N>)>& myF); 

链接器因此抱怨无法解析对构造函数的调用。调用发生在 main()

  ScalarValuedFunction  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)
{}


I am compiling the following code and I am receiving an error. I want to practice templates in boost and I do not know how to handle this problem

#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>);
}

and receiving the following error,

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:\users\university\documents\visual studio 2012\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe : fatal error LNK1120: 1 unresolved externals

please someone tell me what is wrong with my code?

解决方案

You did not provide a definition for your ScalarValuedFunction class template's constructor, which is nevertheless declared in the class template's definition:

ScalarValuedFunction(const boost::function<R (const boost::array<D, N>)> &myF);

The linker therefore complains that the call to the constructor could not be resolved. The call occurs in main():

ScalarValuedFunction<double, double, N> myfun(Norm<double,N>);

To fix your problem, add a definition for the constructor. For instance:

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)
{ }

这篇关于致命错误LNK1120:1未解析外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆