I am trying to utilize an abstract template base class.
The compiler is giving errors in RowArray.cpp that the members rowPntr, and rowSize are "not declared in this scope." Both are protected members from the abstract class AbsRow. I am guessing that this design is not possible because it utilizes virtual functions, which are dynamical bound at run time, but at the same time uses a template which is bound at compile time. Perhaps mixing the two is the issue? What I would like to know is if my design is possible, and why am I getting these compiler errors? I did also forget to mention that when creating a RowArray object RowArray<int> obj(5);
I get link error 2019 in visual studio and in Qt creator it tells me undefined reverence to RowArray constructor and destructor.
Abstract class AbsRow.h
template <typename T>
class AbsRow
{
public:
virtual int getSize()const = 0;
virtual T getValue(int index)const = 0;
protected:
T *rowPntr;
int rowSize;
};
Derived Class RowArray.h
#include "absrow.h"
template <class T>
class RowArray : public AbsRow<T>
{
public:
RowArray(const int rows);
virtual ~RowArray();
virtual T getValue(int index) const override;
virtual int getSize() const override;
void setValue(int row, int value);
};
RowArray.cpp
#include "rowarray.h"
#include <cstdlib>
template <class T>
RowArray<T>::RowArray(const int rows)
{
rowSize = rows;
rowPntr = new int[rows];
for(int index = 0; index < rows; index++)
{
rowPntr[index] = (rand() % 90) + 10;
}
}
template <class T>
RowArray<T>::~RowArray()
{
delete [] rowPntr;
}
template <class T>
void RowArray<T>::setValue(int row, int value)
{
rowPntr[row] = value;
}
template <class T>
int RowArray<T>::getSize() const
{
return rowSize;
}
template <class T>
T RowArray<T>::getValue(int index) const
{
return rowPntr[index];
}
Main
#include "rowarray.h"
int main()
{
RowArray<int> row(7);
}