我做了一个模板类 Grid(我在头文件中说 T 的默认值是浮点数),我引用了源文件的一部分:
#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;
}
其他成员函数可以读取/更改矩阵的值或计算它。
网格.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;
};
我在互联网上发现,为了使用模板类,我需要#include Grid.cpp 和 Grid.h,这样做我可以在 main() 中使用 clas Grid 及其成员函数。我还在 Grid.cpp 周围放置了一个预处理器包装器。
现在,当我尝试构建一个新类 PDEProblem 时,没有继承但使用 Grid 类型的成员,我收到错误:
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
PDE问题.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
}
我怎样才能解决这个问题?在我看来,我对所有相关内容都有默认设置?谢谢