对象声明错误

时间:2014-06-29 19:12:33

标签: c++ matrix

创建类的对象时,我有一个非常奇怪的问题" CorrelatedNormalGenerator"。请参阅下面的代码。 Matrix类由CorrelatedNormalGenerator类使用。在此先感谢您的帮助。

Matrix.h中的

  #include <string>
  #include <vector>

   using namespace std;


   template <typename T, typename I>
   class Matrix
    {

      private:

        vector<vector<T> > M;
        I minRowIndex, maxRowIndex;
        I minColIndex, maxColIndex;
        I rowNumber, colNumber;



       public:

        Matrix();  // default constructor

        Matrix(const I& _rowNumber,   
                const I& _colNumber, 
                 const T& value = 0.0, 
                  const I&   _minRowIndex = 0, 
                    const I& _minColIndex = 0);

        Matrix<T,I>& operator = (const Matrix<T,I>& N);
    Matrix(const Matrix<T,I>& N); // copy constructor,
    virtual  ~Matrix(){};



        T& operator() (const I& row, const I& col);

        const T& operator() (const I& row, const I& col) const;


};

template<typename T, typename I>
Matrix<T,I> :: Matrix(const Matrix<T,I>& N) 
{
   M = N.M;
   rowNumber = N.GetRows();
   colNumber = N.GetCols();
   minRowIndex = N.minRowIndex;
   minColIndex = N.minColIndex;
   maxRowIndex = N.minRowIndex + N.rowNumber - 1;
   maxColIndex = N.minColIndex + N.colNumber - 1;
}



template<typename T, typename I>
Matrix<T,I> :: Matrix( const I& _rowNumber,
                   const I& _colNumber,
                   const T& value,
                   const I& _minRowIndex,
                   const I& _minColIndex)

{
   minRowIndex = _minRowIndex;
   minColIndex = _minColIndex;

   maxRowIndex = _minRowIndex + _rowNumber - 1;
   maxColIndex = _minColIndex + _colNumber - 1;

   rowNumber = _rowNumber;
   colNumber = _colNumber;

M.resize(rowNumber);

for (I i = minRowIndex ; i < rowNumber; i++)
    M[i].resize(colNumber, value);

}

template<typename T, typename I>
T& Matrix<T,I> :: operator() (const I& row, const I& col)
{
   return  M[row][col];
}

// Access the individual elements (const)

template<typename T, typename I>
const T& Matrix<T,I> :: operator() (const I& row, const I& col) const
{
    return  M[row][col];
}


template<typename T, typename I>
Matrix<T,I>& Matrix<T,I> :: operator = (const Matrix<T,I>& N) 
{ 
   rowNumber = N.GetRows();
   colNumber = N.GetCols();
   minRowIndex = N.minRowIndex;
   minColIndex = N.minColIndex;
   maxRowIndex = N.minRowIndex + N.rowNumber - 1;
   maxColIndex = N.minColIndex + N.colNumber - 1;

   M.resize(rowNumber);
   I i,j;

   for (i= minRowIndex; i <= maxRowIndex; i++)
   {
       M[i].resize(colNumber);
   }

   for (i= minRowIndex; i <= maxRowIndex; i++)
   {
        for (j = minColIndex; j <= maxColIndex; j++)
        {
            M[i][j] = N(i,j);
        }
   }

   return *this;
}

在CorrelatedNormalGenerator.h中

#include "Matrix.h"
#include <cmath>
using namespace std;


template <typename T, typename I>   //  I = paramanter of matrix
class CorrelatedNormalGenerator 
{

  private:
         Matrix <T,I> SIGMA; // covariance matrix



  public:

      CorrelatedNormalGenerator(Matrix <T,I>& _SIGMA);


      vector <T> GetCorrVectorSingleAsset(const vector<T>& uncorrVector1, 
                                     const vector<T>&    uncorrVector2);

      vector <T> GetCorrVectorMultiAsset(const vector<T>& uncorrVector);  

      virtual ~CorrelatedNormalGenerator(){};

 };

template <typename T, typename I>
CorrelatedNormalGenerator<T,I> ::  CorrelatedNormalGenerator(Matrix <T,I>& _SIGMA)
{
     SIGMA =_SIGMA;
}



template <typename T, typename I>
vector <T>  CorrelatedNormalGenerator<T,I> :: GetCorrVectorMultiAsset(const vector<T>&    uncorrVector)
{
      Matrix<T,I> Chol = SIGMA.Cholesky();

      return Chol*uncorrVector;
}

template <typename T, typename I>
vector <T>  CorrelatedNormalGenerator<T,I> :: GetCorrVectorSingleAsset(const vector<T>&    uncorrVector1, const vector<T>& uncorrVector2)
 {
      vector<T> corrVector(uncorrVector1.size());

      for (unsigned i = 0; i < uncorrVector1.size(); i++)
          corrVector[i] = rho*uncorrVector1[i] + sqrt(1- rho*rho)*uncorrVector2[i];

      return corrVector;
 }
MAIN.cpp中的

   #include "CorrelatedNormalGenerator.h"

   using namespace std;

   int main()
   {

    Matrix<double, int> P (3,3,0.0);

    P(0,0) = 1.0;
    P(1,1) = 1.0;
    P(2,2) = 1.0;
    P(1,2) = P(2,1) = 0.5;
    P(0,1) = P(1,0) = 0.6;
    P(0,2) = P(2,0) = 0.3;

    CorrelatedNormalGenerator<double, int> gen(P);  // ERROR: MAIN.cpp MAIN.obj : error LNK2019: unresolved external symbol "public: __thiscall Matrix<double,int>::Matrix<double,int>(void)" (??0?$Matrix@NH@@QAE@XZ) referenced in function "public: __thiscall CorrelatedNormalGenerator<double,int>::CorrelatedNormalGenerator<double,int>(class Matrix<double,int> &)" (??0?$CorrelatedNormalGenerator@NH@@QAE@AAV?$Matrix@NH@@@Z) 

    return 0;
   }

1 个答案:

答案 0 :(得分:1)

它告诉您没有实现Matrix默认构造函数,我在您发布的代码中没有看到它。添加

的代码
Matrix<T,I>::Matrix<T,I>()

这是必需的,因为您在CorrelatedNormalGenerator中定义了一个矩阵,而没有使用其他Matrix ctors之一在初始化列表中初始化它,因此需要默认的ctor:

private:
     Matrix <T,I> SIGMA; // covariance matrix