1

I am writing member functions for a Stack class. I have a linked list (LL) nested-class as a member of the Stack class. In the Stack constructor, I instantiate a new linked list which calls the LL constructor. Using the LL's member functions I generate a new node and point it at the 1st stack. This resolves fine.

However, when I am coding a Stack member function eclipse no longer resolves the LL instance generated in the Stack constructor nor the LL member function that I am trying to call.

I have tried switching the nested class between private and public member designations. I also tried to connect the nested-class (LL) with it enclosing/parent class (Stack) by making the enclosing class a member of the nested class like in the previous question/response:

Nested Class member function can't access function of enclosing class. Why?

Neither have had an effect

No problems here:
class Stack
{
private:
   int height, top_element;
   class LL
   {
      push_front(string* new_address)
      {
         // ... definition 
      }
      //...  more nested-class members
   };
public:
   Stack(); // constructor
   void push(string); // enclosing class member
};

Stack constructor is fine as well:

Stack::Stack(int size)
{
    height = size;
    top_element = 0;   

    string stack[height];    
    LL stack_map;
    stack_map.push_front(stack);
}

When I get here I encounter my problem:

void Stack::push(string data)
{
    if (top_element == height - 1)
    {
        string stack[height];           
        stack_map.push_front(stack); // <- PROBLEM
    }
}

I hope I did not include too much code. The second block is the demonstrate that the constructor instantiated the LL and called push_front() no problem while the very next definition complained about the same function and couldn't recognize the instantiated LL, stack_map

stack_map and push_front are both underlined in red

Symbol 'stack_map' could not be resolved

and

Method 'push_front' could not be resolved
JeJo
  • 30,635
  • 6
  • 49
  • 88
MagicGAT
  • 135
  • 7

2 Answers2

3

You have mainly two problems!

  1. You have an object of LL class in the constructor of Stack class, which is local to the scope, and not available in the member function push!

    You have

    LL stack_map;
    

    in the scope of the constructor of the class (i.e. Stack::Stack(int size) {...}) not in the member functions void Stack::push(string data) of the class Stack.

    Therefore, when you do

       stack_map.push_front(stack);
    

    in the member function (i.e. Stack::push), it is not known to the compiler and hence the error.

    If you want to access the same object of LL class (i.e. stack_map) in other member functions of the Stack class, you need to keep it as a member variable of the class.

  2. Secondly, variable length of arrays are not part of the standard C++. Meaning, the code in the constructor of stack

    string stack[height];
    

    should be changed. The better alternative is std::vector of std::string's.

Meaning, change to

#include <vector> // std::vector

class Stack
{
private:
   int height, top_element;
   class LL
   {
   public:
      void push_front(const std::vector<std::string>& new_address) { /* code here */ }
      //              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--->> vector of `std::string`
   };
   LL  stack_map; // >>>> `stack_map` member of `Stack`
public:
   Stack(int size)
      : height{ size }
      , top_element{ 0 }
   {
      std::vector<std::string> stack(height);
      stack_map.push_front(stack); //>>>> push_back to the member `stack_map`
   }

   void push(const std::string& str)
   {
      if (top_element == height - 1)
      {
         std::vector<std::string> stack(height);
         stack_map.push_front(stack); //>>>> push_back to the member `stack_map`
      }
   }
};
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Thank you. Late reply but if I set the Stack member `height` to a constant, couldn't I use that as the array size? It is an int. It is a requirement that the size of stacks is determined by the user and I am also required to generate more stacks (arrays) as the user fills arrays. For that reason I am not using vectors, otherwise your suggestion would make perfect sense. Either way, thank you – MagicGAT Oct 26 '19 at 19:20
  • @MagicGAT You can use `string stack[height];`, only when `height` compile-time constants. The [`const` itself is not sufficient](https://stackoverflow.com/questions/27048025/is-variable-defined-by-const-int-determined-at-compilation-time) for that. "*I am also required to generate more stacks (arrays) as the user fills arrays*": meaning the size of the array is determined at run-time. You could do definitely `string stack = new string[height];` with extra housekeeping works for managing the memory you manually allocated. But, would we prefer to do in C++ that, when we have the `std::vector`? – JeJo Oct 26 '19 at 20:17
1

The second block is the demonstrate that the constructor instantiated the LL and called push_front() no problem while the very next definition complained about the same function and couldn't recognize the instantiated LL, stack_map.

You have a variable named stack_map in the onstructor but it is only a function local variable. It is not a member of the class. Because of that, it doesn't exist in push().

Sounds like you need to make that a member variable of the class.

class Stack
{
  private:
    int height, top_element;
    class LL
    {
      push_front(string * new_address)
      {
        ... ; // definition 
      }
    };

    LL stack_map;

  public:
    Stack(); // constructor
    void push(string); // enclosing class member
};

After you do that, make sure to remove the function local variable from the constructor.

R Sahu
  • 204,454
  • 14
  • 159
  • 270