Home > Software design >  cannot access private member in the same class
cannot access private member in the same class

Time:11-14

I tried to declare a public member function with a private struct, but it didn't work. Can someone help me with this? Here's the header file

class LinkedList
{
public:
   LinkedList();
   ~LinkedList();
   ...
   //I tried to add LinkedList also not working
   //void deleteNode(const LinkedList::Node* n);
   void deleteNode(const Node* n);

private:
    struct Node
    {
        std::string value;
        Node *next;
    };

CodePudding user response:

class LinkedList
{
public:
   LinkedList();
   ~LinkedList();

   void deleteNode(const Node* n);

private:
    struct Node
    {
        std::string value;
        Node *next;
    };
};

Node is declared after void deleteNode(const Node* n);, so the compiler won't know what Node is.

You should do this instead:

class LinkedList
{
private:
    struct Node
    {
        std::string value;
        Node *next;
    };

public:
   LinkedList();
   ~LinkedList();

   void deleteNode(const Node* n);

};
  • Related