Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 致命错误LNK1120:1个未解析的外部_C++_Templates - Fatal编程技术网

C++ 致命错误LNK1120:1个未解析的外部

C++ 致命错误LNK1120:1个未解析的外部,c++,templates,C++,Templates,我正在编译以下代码,收到一个错误。我想在boost中练习模板,但我不知道如何处理这个问题 #include <stdafx.h> #include <iostream> #include <string> #include <boost/function.hpp> #include <boost/array.hpp> using namespace std; template<typename R,typename D>

我正在编译以下代码,收到一个错误。我想在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>);
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
模板
类泛型函数
{
私人:
boost::函数f;
受保护的:
泛型函数();
公众:
GenericFunction(const boost::function和myFunction);
R评估(常数D和值)常数;
R运算符()(常量D和值)常量;
};
模板
类ScalarValuedFunction:公共泛型函数
{
公众:
ScalarValuedFunction(constboost::function&myF);
};
模板
数值范数(常量boost::数组和向量)
{
数值结果=Vec[0]*Vec[0];
对于(std::size\u t i=1;ic:\users\university\documents\visual studio 2012\Projects\ConsoleApplication2\Debug\ConsoleApplication2.exe):致命错误LNK1120:1未解析的外部

请有人告诉我我的代码有什么问题?

您没有为
ScalarValuedFunction
类定义构造函数,只是声明了它。

您没有为
ScalarValuedFunction
类定义构造函数,只是声明了它。

您没有为
ScalarValuedFunction
提供定义>类模板的构造函数,但在类模板的定义中声明:

ScalarValuedFunction(const boost::function<R (const boost::array<D, N>)> &myF);
若要解决问题,请为构造函数添加定义。例如:

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您没有为
ScalarValuedFunction
类模板的构造函数提供定义,但该构造函数在类模板的定义中声明:

ScalarValuedFunction(const boost::function<R (const boost::array<D, N>)> &myF);
若要解决问题,请为构造函数添加定义。例如:

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您为
ScalarValuedFunction
声明了一个构造函数,但没有定义它。当然,一旦这样做,您还需要为
GenericFunction
定义构造函数。您为
ScalarValuedFunction
声明了一个构造函数,但没有定义它。当然,一旦这样做,您还需要定义
泛型函数的构造函数。谢谢,这就解释了lot@user2085646当前位置很高兴它能帮上忙谢谢,这就解释了lot@user2085646字体很高兴它有帮助