写入位置0x00000000时发生访问冲突.指针问题

Access violation writing location 0x00000000. problems with pointers

本文关键字:访问冲突 指针 问题 位置 0x00000000      更新时间:2023-10-16

我在编写一些赋值代码时遇到问题。该项目基于大学生在线竞赛问题档案。

这是头文件:

#include <iostream>
#include <cmath>
using namespace std;
class Vito
{
public:
    Vito(int relative_count); //constructor
    ~Vito(); //destructor
    int min(); //finds minimum "distance" between vito's relatives
private:
    int *streets; //pointer to array of street numbers
    int sum(int index); //generates sum of distances for each of vito's relatives
    void getStreetNums(); //retrieves street numbers from user
    int relatives; //used globaly to set number of repititions
};
Vito::Vito(int relative_count = 0)
{
    int *streets = new int[relative_count]; //allocates memory for array streets
    relatives = relative_count;
    getStreetNums();
}
Vito::~Vito()
{ delete [] streets; } //releases memory used by class
void Vito::getStreetNums()
{
    cout << "Enter all street numbers, seperated by a space: ";
    int street_num;
    for (int i = 0; i < relatives; i++)
    { 
        cin >> street_num; 
        streets[i] = street_num;
    }
}
int Vito::min()
{
    int MIN = 65546, test_distance; //initialized to maximum possible value for an integer in C++
    for (int i = 0; i < relatives; i++)
    {
        test_distance = sum(i);
        if( MIN > test_distance )
        { MIN = test_distance; }
    }
    return MIN;
}
int Vito::sum(int index)
{
    int SUM = 0, street_num;
    street_num = *(streets+index); //set value for determining distances between one house and the others
    for (int i = 0; i < relatives; i++)
    { SUM += abs( street_num - streets[i] ); }
    return SUM;
}

主要内容如下:

#include <iostream>
#include "proj_02.h"
using namespace std;
int main()
{
    int relatives, tests;
    cout << "This program will supply a minimum distance between homes based on a given number of relatives and street numbers their homes are on. All entered values must be integers." << endl << endl;
    cout << "Enter how many tests will be run: "; 
    cin  >> tests;
    for (int i = 0; i < tests; i++)
    {
        cout << "Enter how many relatives will be used in test " << i+1 << ": ";
        cin  >> relatives;
        Vito family(relatives);
        cout << "For this case, the minimum distance between each relatives house compared to each other is: " << family.min() << endl << endl;
    }
}

执行后,我收到一条错误消息,上面写着"0xC0000005:写入位置0x00000000的访问违规。"

void Vito::getStreetNums()
{
    cout << "Enter all street numbers, seperated by a space: ";
    int street_num;
    for (int i = 0; i < relatives; i++)
    { 
        cin >> street_num; 
        streets[i] = street_num;
    }
}

调试显示为街道设置了一个空内存地址,但我在构造函数的前面为它分配了内存。有人能解释一下这里发生了什么吗?

Vito::Vito(int relative_count = 0)
{
    int *streets = new int[relative_count]; //allocates memory for array streets

在构造函数中,您定义了一个局部变量streets,并为其分配内存。局部变量streets隐藏类成员streets。更改为:

Vito::Vito(int relative_count = 0)
{
    streets = new int[relative_count]; //allocates memory for array streets