0

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?

kevinuulong
  • 530
  • 1
  • 4
  • 17
  • Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) This might not fix the above error. But this should be addressed first. – Richard Critten Mar 03 '23 at 23:11
  • Why not simply make the members be `protected` instead of `private`? Then `DerivedList` will have access without needing to be a `friend`. Also, [you can't split template classes into `.h` and `.cpp` files](https://stackoverflow.com/questions/495021/), like you are trying to do. – Remy Lebeau Mar 03 '23 at 23:11
  • Why don't you use protected section instead of private? – Murat Hepeyiler Mar 03 '23 at 23:12
  • 2
    You must use `this->m_data` in the derived class. This has nothing to do with private, friends, etc... Plenty of dupes. – Sam Varshavchik Mar 03 '23 at 23:13
  • 1
    Qualifying it as `List::m_data` should also work, too – Remy Lebeau Mar 03 '23 at 23:16

0 Answers0