I made a template class Grid(where i said in the header file that the default for T is float), i quoted a part of the source file:
#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;
}
And the other member functions can read/change a value of matrix or cout it.
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;
};
I found on the internet that in order to use a template class I needed to #include Grid.cpp as well as Grid.h, and doing this I can use the clas Grid and its member functions in my main(). I also put a preprocessor wrapper arround Grid.cpp.
Now, when I try to build a new class PDEProblem, without inheritance but using members from type Grid I get errors:
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
}
How can I fix this? It seems to me that I have defaults for everything relevant? Thank you
With my compiler (Visual Studio 2010) and your code, I can make your error go away by moving the default parameter values from the function definition to the function prototype. Specifically:
Grid.h
template<typename T=float> class Grid{
public:
Grid(unsigned int rows = 1, unsigned int columns = 1);
...
};
Grid.cpp
template <class T>
Grid<T>::Grid(unsigned int rows, unsigned int columns)
:Rows(rows),Columns(columns)
{
reset();
}