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
You have mainly two problems!
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.
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`
}
}
};