I have two classes a List
and a DerivedList
where I want the DerivedList
class to be able to access the private members of the List
class. According to this answer I can do this by making the DerivedList
a friend of the List
class.
I think my problem occurs because both my List
and DerivedList
classes need to have templates. When I compile my code (below) I get the following error(s):
DerivedList.cpp: In member function ‘void DerivedList<T, size>::addItem(T*)’:
DerivedList.cpp:5:3: error: ‘m_data’ was not declared in this scope
5 | m_data[size - m_length - 1] = item;
| ^~~~~~
DerivedList.cpp:5:17: error: ‘m_length’ was not declared in this scope
5 | m_data[size - m_length - 1] = item;
| ^~~~~~~~
DerivedList.cpp: In member function ‘T* DerivedList<T, size>::removeItem(T*)’:
DerivedList.cpp:11:3: error: ‘m_data’ was not declared in this scope
11 | m_data[size - m_length] = nullptr;
| ^~~~~~
DerivedList.cpp:11:17: error: ‘m_length’ was not declared in this scope
11 | m_data[size - m_length] = nullptr;
| ^~~~~~~~
Here is my current code:
List.h
template <class T, int size>
class DerivedList;
template <class T, int size>
class List {
friend class DerivedList<T, size>;
private:
T** m_data[size];
int m_length{0};
public:
List(){};
virtual void addItem(T* item);
virtual T* removeItem(T* item);
};
List.cpp
#include "List.h"
template <class T, int size>
void List<T, size>::addItem(T* item) {
m_data[m_length] = item;
m_length++;
};
template <class T, int size>
T* List<T, size>::removeItem(T* item) {
m_data[m_length - 1] = nullptr;
m_data--;
};
DerivedList.h
#include "List.h"
template <class T, int size>
class DerivedList : public List<T, size>{
public:
DerivedList() : List<T, size>(){};
void addItem(T* item);
T* removeItem(T* item);
};
DerivedList.cpp
#include "DerivedList.h"
template <class T, int size>
void DerivedList<T, size>::addItem(T* item) {
m_data[size - m_length - 1] = item;
m_length++;
};
template <class T, int size>
T* DerivedList<T, size>::removeItem(T* item) {
m_data[size - m_length] = nullptr;
m_data--;
};
What am I doing wrong and/or missing?