c++ - 不解析嵌套类实例或函数的类函数

标签 c++ class c++11 inner-classes member-functions

我正在为 Stack 类编写成员函数。我有一个链表 (LL) 嵌套类作为 Stack 类的成员。在 Stack 构造函数中,我实例化了一个调用 LL 构造函数的新链表。使用 LL 的成员函数,我生成了一个新节点并将其指向 1st 堆栈。这解决得很好。

但是,当我编写一个 Stack 成员函数时,eclipse 不再解析 Stack 构造函数中生成的 LL 实例,也不再解析 >LL 我试图调用的成员函数。

我已经尝试在 privatepublic 成员指定之间切换嵌套类。我还尝试通过使封闭类成为嵌套类的成员来将嵌套类 (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
    }
}

我希望我没有包含太多代码。第二个 block 是证明构造函数实例化了 LL 并调用了 push_front() 没有问题,而下一个定义提示相同的函数并且无法识别实例化LL, stack_map

stack_mappush_front 都带有红色下划线

Symbol 'stack_map' could not be resolved

Method 'push_front' could not be resolved

最佳答案

你主要有两个问题!

  1. Stack 的构造函数中有一个 LL 类的对象 类,在范围内是局部的,在成员中不可用 函数 push!

    你有

    LL stack_map;
    

    在类的构造函数范围内(即 Stack::Stack(int size) {...})不在成员函数 void Stack::push(string数据)Stack

    因此,当你这样做的时候

       stack_map.push_front(stack);
    

    在成员函数中(即 Stack::push),编译器不知道它,因此会出错。

    如果你想访问 LL 类的同一个对象(即 stack_map)在Stack类的其他成员函数中,需要将其作为类的成员变量保存。

  2. 其次,variable length of arrays are not part of the standard C++ .意思是,stack

    的构造函数中的代码
    string stack[height];
    

    应该改变。更好的选择是 std::vector std::string

意思,改成

#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`
      }
   }
};

关于c++ - 不解析嵌套类实例或函数的类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58566450/

相关文章:

c++ - 在c++中合并两个wav文件时错误的持续时间信息

python - 让两个 Python 脚本一起工作

c++ - 为函数创建 openmp 线程

c++ - 指向类数据成员 "::*"的指针

c++ - 在 QT5 中将信号连接到 c++11 lambda

c++ - 我如何在继承构造函数上做正确的 SFINAE?

c++ - Unix 文件描述符

c++从重复数组创建子数组

c++ - vector 中的删除函数不会删除指针?

C++: 错误: ‘class’ 没有名为的成员