テンプレート クラス Grid を作成し (ヘッダー ファイルで、T のデフォルトは float であると記述しました)、ソース ファイルの一部を引用しました。
#include"Grid.h"
template <class T>
Grid<T>::Grid(unsigned int rows=1, unsigned int columns=1)
:Rows(rows),Columns(columns)
{
reset();
}
template<class T>
Grid<T>::~Grid(){}
template <class T>
void Grid<T>::reset()
{
vector<T> vec(Rows * Columns,T());
matrix = vec;
}
また、他のメンバー関数は、行列の値を読み取り/変更したり、計算したりできます。
Grid.h:
template<typename T=float> class Grid{
public:
Grid(unsigned int, unsigned int);
~Grid();
T getValue(unsigned int, unsigned int);
void setValue(unsigned int, unsigned int, T);
void reset();
void write();
private:
unsigned int Rows;
unsigned int Columns;
vector<T> matrix;
};
テンプレート クラスを使用するには、Grid.cpp と Grid.h を #include する必要があることをインターネットで見つけました。これを行うと、クラス Grid とそのメンバー関数を main() で使用できます。また、Grid.cpp の周りにプリプロセッサ ラッパーを置きます。
ここで、継承なしで Grid 型のメンバーを使用して新しいクラス PDEProblem を構築しようとすると、エラーが発生します。
Error 2 error C2512: 'Grid<>' : no appropriate default constructor available c:\users\... 15
Error 3 error C2512: 'Grid<T>' : no appropriate default constructor available c:\users\... 15
4 IntelliSense: no default constructor exists for class "Grid<float>" c:\Users\... 15
PDEProblem.h:
#include"grid.h"
#include"grid.cpp"
class PDEProblem: Grid<>
{
public:
PDEProblem(unsigned int,unsigned int);
~PDEProblem();
//some more other data members
private:
Grid<char> gridFlags;
Grid<> grid;
unsigned int Rows;
unsigned int Columns;
void conPot(unsigned int, unsigned int);
void conFlag(unsigned int, unsigned int);
};
PDEProblem.cpp:
#include"grid.h"
#include"grid.cpp"
#include "PDEProblem.h"
PDEProblem::PDEProblem(unsigned int rows=1,unsigned int columns=1)
:Rows(rows), Columns(columns)
{
conPot(rows, columns);
conFlag(rows,columns);
}
PDEProblem::~PDEProblem(){}
void PDEProblem::conPot(unsigned int rows, unsigned int columns)
{
grid=Grid<>(rows,columns);
}
void PDEProblem::conFlag(unsigned int rows, unsigned int columns)
{gridFlags=Grid<char>(rows,columns);
// some stuff with a few if and for loops which sets some elements of gridFlags to 1 and the others to 0
}
どうすればこれを修正できますか? 関連するすべてのデフォルトがあるように思えますか?ありがとうございました