【问题标题】:handling arrays in class declaration [closed]在类声明中处理数组[关闭]
【发布时间】:2015-04-18 09:45:07
【问题描述】:

我刚刚在一个类中编写了堆栈数据结构的简化实现,但整数数组的处理方式我似乎无法理解。

与push() 中相同的 sn-p 代码将给出我期望的行为,但在此程序中,在某个数组位置分配一个值会将值分配给索引变量>

 #include <iostream>
using namespace std;

class stack
{
public:
    stack(int size)
    {
        ar_size = size - 1;
        array[ar_size];
        index = 0;
    }
    void push(int value)
    {
        cout << "index; " << index << endl; //will output 0
        cout << "value: " << value << endl; //will output 8
        array[index++] = value;
        cout << "index; " << index << endl; //will output 8
        cout << "value: " << value << endl; //will output 8
        cout << "array: " << array[index] << endl; //will output what seems to be a memory address
    }
    int pop()
    {
        cout << "index; " << index << endl; //will output 8
        return array[index--];
    }
private:
    int ar_size;
    int array[];
    int index;
};
int main()
{
    stack tower(64);
    tower.push(8);
    int r = tower.pop();
    cout << "pop: " << r << endl; //will output what seemed to be a memory address

    return 0;
}

【问题讨论】:

  • 这个语句实际上应该做什么:array[ar_size];?我有一种感觉,它并没有像你想象的那样做。
  • 启用编译器警告 + 阅读并理解警告 = 利润!
  • @πάντα ῥεῖ 我明白你的意思,我在私有定义中声明数组大小时遇到​​了一个错误,最终犯了这个错误。那么,我应该如何以及在哪里尝试声明数组大小?
  • @maja 最好使用std::vector&lt;int&gt;,并在构造函数成员初始化列表中使用ar_size对其进行初始化:std::vector&lt;int&gt; array;stack(int size) : array(size) { // ...
  • @Hiroaki 我不确定我应该期待什么结果,如果我在构造函数中尝试这个程序会崩溃,如果我在私有部分尝试它,我会得到与尝试设置相同的错误 @ 987654328@ with a size: 无效使用非静态数据成员 stack::ar_size`。

标签: c++ arrays class


【解决方案1】:

这是您示例的更正代码:

#include <iostream>

class stack
{
public:
   stack(int size)
   {
      ar_size = size - 1;
      array = new int[size];
      index = 0;
   }
   void push(int value)
   {
      array[index++] = value;
   }
   int pop()
   {
      return array[--index];
   }
   ~stack()
   {
      delete array;
   }
private:
   int ar_size;
   int *array;
   int index;
};

int main()
{
   stack tower(64);
   tower.push(8);
   int r = tower.pop();
   std::cout << "pop: " << r << std::endl; //Will output 8 :)

   return 0;
}

它有几个问题。

  1. 正如您在构造函数中的 cmets array[ar_size]; 中指出的那样,它没有按照您的意愿进行操作。 array[ar_size]; 访问给定索引处的数组,它不会为您分配数组。我已经解决了这个问题,现在数组通过new 分配并在堆栈被销毁时删除。
  2. return array[index--]; 也不对。您需要在访问元素之前降低索引。 return array[--index]; 现在是对的。
  3. 您缺少一堆检查,因此您的堆栈不会导致segfault 或任何其他未定义的行为。您需要检查您是否仍然可以推送值或是否可以弹出值等等。

我希望它可以澄清一点。

【讨论】:

  • 我自己没有发现的一件事是我需要使用堆来处理类函数之间的数组,我将据此更改标题..(对我来说评估段错误还为时过早)。
  • 您不需要使用堆。但是您不能以这种方式在堆栈上动态分配内存。如果您愿意,可以使用 some ways 来完成,但这样会容易得多。
  • 我将不得不阅读一段时间,但现在堆似乎已经足够好了。谢谢。
【解决方案2】:

你可以使用动态内存分配。像这样

private:
int ar_size;
int *array;//pointer to array
int index;

然后在构造函数中

stack(int size)
{
    ar_size = size - 1;
    array=new int[ar_size];
    index = 0;
}

由于这是动态内存分配,请确保释放分配的内存。您可以使用析构函数

~stack()
{
  delete[] array;
}

另一点是在你推入一个元素之后,你将index增加了1。所以现在索引确实指向堆栈中的下一个插入点。所以如果你做一个弹出操作,它会从中删除一个元素index 位置,但那里还没有元素。所以你可以将你的 pop 函数更改为

int pop()
{
    cout << "index; " << index << endl; //will output 8
    return array[--index];//now index will point to the top element
}

【讨论】:

  • 您在某处缺少delete。这将导致内存泄漏。一个可以清理的析构函数会很好。
  • 是的。错过了。感谢您指出。
【解决方案3】:

我想你想要array = new int[ar_size]; 而不是array[ar_size];。然后你需要创建一个析构函数来执行delete [] array;。

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多