c++ - 如何在构造函数中初始化struct的动态数组?

标签 c++ arrays data-structures struct

这是一个基于动态数组结构的 Stack 类,用于深度优先搜索 (DFS)。程序一遇到push()函数就跑不起来,说明构造函数中数组没有初始化成功。

我试图寻找错误,甚至将结构的动态数组更改为并行数组,但它仍然不起作用。如果问题看起来太简单而无法解决,我深表歉意,因为我没有扎实的 C++ 基础。

#include <iostream>
#include <iomanip>
#ifndef HEADER_H
#define HEADER_H

using namespace std;

struct Value
{
    int row;  // row number of position
    int col;  // column number of position

    //operator int() const { return row; }
};

class ArrayStack
{

public:
    int top;
    Value* array;
    ArrayStack();
    bool isEmpty();
    bool isFull();
    void push(int r, int c);
    void pop();
    int poprowvalue(int value);
    int popcolvalue(int value);
    int peekrow(int pos);
    int peekcol(int pos);
    int count();
    void change(int pos, int value1, int value2);
    void display();
    void resize();
private:
    int size;
};

ArrayStack::ArrayStack()
{
    //Initialize all variablies
    top = -1;
    size = 10;
    Value * array = new Value[size];
    for (int i = 0; i < size; i++)
    {
        array[i].row = 0;
        array[i].col = 0;
    }
}

bool ArrayStack::isEmpty()
{
    if (top == -1)
        return true;
    else
        return false;
}

bool ArrayStack::isFull()
{
    if (top == size - 1)
        return true;
    else
        return false;
}

void ArrayStack::resize()
{
    if (isFull())
        size *= 2;
    else if (top == size / 4)
        size /= 2;
}

void ArrayStack::push(int r, int c)
{
    if (isEmpty() == false)
        resize();
    array[top + 1].row = r;
    array[top + 1].col = c;
    top++;
}

void ArrayStack::pop()
{
    int value;

    if (isEmpty())
    {
        cout << "Stack underflow" << endl;
    }
    else
    {
        poprowvalue(array[top].row);
        popcolvalue(array[top].col);
        array[top].row = 0;
        array[top].col = 0;
        top--;
    }
}

int ArrayStack::poprowvalue(int v)
{
    return v;
}

int ArrayStack::popcolvalue(int v)
{
    return v;
}

int ArrayStack::peekrow(int pos)
{
    if (isEmpty())
        cout << "Stack underflow" << endl;
    else
        return array[pos].row;
}

int ArrayStack::peekcol(int pos)
{
    if (isEmpty())
        cout << "Stack underflow" << endl;
    else
        return array[pos].col;
}

int ArrayStack::count()
{
    return (top + 1);
}

void ArrayStack::change(int pos, int value1, int value2)
{
    if (isEmpty())
        cout << "Stack underflow" << endl;
    else
    {
        array[pos].row = value1;
        array[pos].col = value2;
    }

}

void ArrayStack::display()
{
    for (int i = size - 1; i > -1; i--)
    {
        cout << array[i].row << "  " << array[i].col << endl;
    }


}

#endif 

我希望它运行良好,但在第 80 行总是抛出异常,如下所示:

Exception thrown at 0x00007FF6A160487C in Assignment1.exe: 0xC0000005: Access violation writing location 0x0000000000000000.

最佳答案

问题是这里的这一行:

Value * array = new Value[size];

这声明了一个新的 array 变量。您分配的是 array,而不是您的成员变量 array

答案很简单,改成这样就可以了:

array = new Value[size];

关于c++ - 如何在构造函数中初始化struct的动态数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58127052/

相关文章:

javascript - 根据数组元素的总和对数组中的元素进行排序

c++ - C++ 标准库是静态库吗?

c++ - 线程断点?

java - 创建一个 Student 将其传递给 SQL 对象并在线程 "AWT-EventQueue-0"lang.ArrayIndexOutOfBoundsException : 4 中获取异常

c++ - 我如何像素化一维数组

c - 求给定数组中除 1 个整数之外的所有整数的最大和最小总和

c++ - 我有一段代码从 FILETIME 结构中获取持续时间。这是什么意思?

c++ - 使用 CIMG for c++ 设置线条图案

c++ - 将基于堆的 N 元数组引用传递给函数

javascript - 返回发行专辑最多的年份的函数