0

Just started learning c++ for a class, I can't figure out what is wrong with this code! I'm making a stack class with a helper class nested inside it called node that acts as a linked list. The error I'm getting is on line 12 and is:

Stack.cpp: In destructor ‘Stack::~Stack()’: Stack.cpp:12:24: error: request for member ‘getNext’ in ‘((Stack*)this)->Stack::node’, which is of non-class type ‘Stack::Node*’

Here's my code:

#include "Stack.h"

Stack:: Stack ()
{
  height = 0;
  node = 0;
}

Stack:: ~Stack()
{
  while(node != 0){
    Node *next = *node.getNext();
    delete node;
    node = next;
  }
  node = 0;
}

And Here's my header file:

using namespace std;

class Stack
{
 private:
  int height;
  class Node{
    private:
      int data;
      Node* next;
    public:
      void setData(int x){
        data = x;
      }
      void setNext(Node* x){
        next = x;
      }
      int getData(){
        return data;
      }
      Node* getNext(){
        return next;
      }
    };
  Node* node;
public:
  Stack();
  ~Stack();
  void push(int x);
  int pop();
  int peek();
  int getHeight();
  bool isEmpty();
};
1

1 Answer 1

3
 Node *next = *node.getNext();

should be

Node *next = (*node).getNext();

Since . operator has higher precedence than * deference operator.

You can also use:

Node *next = node->getNext();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.