samedi 27 octobre 2018

Friendship between source files c++

I'm working on a Binary Search Tree, and part of the requirement is that we have to use reference to pointers. As part of that, we are required to use friendship between our BST class and Node class. However, after I include friend class BST; in the Node class, I still can't access private members in Node.

Example error: 'BST.cpp:97:32: error: 'int Node::data' is private within this context' when calling node->data.

What am I missing?

BST.h:

#include "BSTInterface.h"
#include "Node.h"
class BST : public BSTInterface
{
private:
  Node* rootNode;
  Node* find(int data);
public:
  BST() : rootNode(NULL) {}
  ~BST() {}
  Node* getRootNode() const override;
  bool add(int data) override;
  bool remove(int data) override;
  void clear() override;
};

Node.h:

#include "NodeInterface.h"

class Node : public NodeInterface
{
private:
  int data;
  Node* leftChild;
  Node* rightChild;
  friend class BST;
public:
  Node(int data) : data(data), leftChild(NULL), rightChild(NULL) {}
  ~Node() {}
  int getData() const override;
  Node* getLeftChild() const override;
  Node* getRightChild() const override;
};

Aucun commentaire:

Enregistrer un commentaire