0

次のコードをコンパイルしていますが、エラーが発生します。ブーストでテンプレートを練習したいのですが、この問題の処理方法がわかりません

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

誰かが私のコードの何が問題なのか教えてください。

4

2 に答える 2

2

クラス テンプレートのコンストラクターの定義を提供していませんが、それにもかかわらず、クラス テンプレートの定義で宣言されています。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)
{ }
于 2013-02-21T01:57:18.187 に答える
1

クラスのコンストラクターを定義しておらずScalarValuedFunction、宣言しただけです。

于 2013-02-21T01:57:11.007 に答える