【发布时间】:2019-10-25 22:25:40
【问题描述】:
我正在为Stack 类编写成员函数。我有一个链接列表 (LL) 嵌套类作为 Stack 类的成员。在Stack 构造函数中,我实例化了一个调用LL 构造函数的新链表。使用LL 的成员函数,我生成了一个新节点并将其指向第一个st 堆栈。这很好解决。
但是,当我编写 Stack 成员函数时,eclipse 不再解析在 Stack 构造函数中生成的 LL 实例,也无法解析我试图调用的 LL 成员函数。
我尝试在 private 和 public 成员名称之间切换嵌套类。我还尝试将嵌套类 (LL) 与它的封闭/父类 (Stack) 连接起来,方法是使封闭类成为嵌套类的成员,就像在上一个问题/响应中一样:
Nested Class member function can't access function of enclosing class. Why?
都没有影响
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 构造函数也可以:
Stack::Stack(int size)
{
height = size;
top_element = 0;
string stack[height];
LL stack_map;
stack_map.push_front(stack);
}
当我到达这里时,我遇到了我的问题:
void Stack::push(string data)
{
if (top_element == height - 1)
{
string stack[height];
stack_map.push_front(stack); // <- PROBLEM
}
}
我希望我没有包含太多代码。第二块是证明构造函数实例化了LL并调用push_front()没有问题,而下一个定义抱怨相同的函数并且无法识别实例化的LL,stack_map
stack_map 和 push_front 都带有红色下划线
Symbol 'stack_map' could not be resolved
和
Method 'push_front' could not be resolved
【问题讨论】:
-
string stack[height]是无效的 C++(使用 VLA 扩展)。 -
LL stack_map;是构造函数中的局部变量,它不是类成员。
标签: c++ class c++11 inner-classes member-functions