gpt4 book ai didi

C++,为什么我使用这种数组存储数据时程序会挂起?

转载 作者:行者123 更新时间:2023-11-28 07:53:44 25 4
gpt4 key购买 nike

当我尝试为 x pt.1 输入任何坐标时,我的程序只是简单地挂起。

谁能帮帮我??

// shape.h
#include <string>
#ifndef SHAPE_H
#define SHAPE_H 1
using namespace std;

class Shape
{

protected:
int x, y;
string name;
public:
// a simple inline constructor
Shape(int new_x, int new_y, string new_name): x(new_x), y(new_y), name(new_name)
{
return;
};

virtual ~Shape()
{
return;
};
// inline getter/setter functions
string getName() { return name; };
void setName(string new_name)
{
name = new_name;
}

int getX() { return x; };
void setX(int set_x)
{
x = set_x;
}

int getY() { return y; };
void setY(int set_y)
{
y = set_y;
}

void toString();
};
#endif

形状标题。这个程序也是关于继承的..

// square.h
#ifndef SQUARE_H
#define SQUARE_H 1
#include <string>
#include "Shape.h"
using namespace std;

class Square : public Shape
{
protected:
int size;
public:
// a c'tor that calls the parent class's c'tor
Square(int new_x, int new_y, string new_name): Shape(new_x, new_y, new_name)
{
return;
};

void setXY();
Square *arraySquare[1];
};

void Square::setXY()
{
int count = 0;
for(int i=0; i<4; i++)
{
cout<<"Please enter x-ordinate of pt. "<<i+1<<" : ";
cin>>x;
arraySquare[count]->setX(x);
cout<<"Please enter y-ordinate of pt. "<<i+1<<" : ";
cin>>y;
arraySquare[count]->setY(y);
count++;
}
}

#endif

方形标题..形状的子类..

#include <iostream>
#include <string>
#include "Shape.h"
#include "Square.h"
using namespace std;

class Main
{
public:
void mainMenu();
char menuChoice;
void stringToUpper(string &s);
};

void stringToUpper(string &s)
{
for(unsigned int l = 0; l < s.length(); l++)
{
s[l] = toupper(s[l]);
}
}

void Main::mainMenu()
{
cout<<"Welcome to Main program!"<<endl<<endl;
cout<<"1) Input data"<<endl;
cout<<"2) 2"<<endl;
cout<<"3) 3"<<endl;
cout<<"4) 4"<<endl;
cout<<"Q) Enter 'Q' to quit"<<endl<<endl;
}

int main()
{
char menuChoice;

bool quit=false;
Main main;
Square *square;

string shape, special;

while ( !quit )
{
main.mainMenu();
cout<<"Please enter your choice : ";
cin>>menuChoice;
menuChoice = toupper(menuChoice);

switch(menuChoice)
{
case '1':
cout<<endl<<"[ Input data ]"<<endl;
cout<<"Please enter name of shape : "<<endl;
cin>>shape;
stringToUpper(shape);
if(shape=="SQUARE")
{
square->setXY();
}
break;
case '2':
cout<<"Print"<<endl<<endl;
break;
case '3':
cout<<"You choosen 3"<<endl<<endl;
break;
case '4':
cout<<"You choosen 4"<<endl<<endl;
break;
case 'Q':
cout<<"You have chosen to quit!"<<endl<<endl;
quit=true;
exit(0);
default:
cout<<"Invalid entry!"<<endl<<endl;
break;
}
}
}

这是程序的外观..

每当我运行它并输入第一个坐标 x 时,它就会挂起。谁能帮忙?

最佳答案

您的程序挂起是因为它访问了未分配的对象。每次看到指针声明时,必须应用 3 条规则:a) 是否设置为默认值? b) 是否已分配/指定? c) 是否被删除?

在 Square 类中,您已经声明了这个数据成员:

Square *arraySquare[1];

因此,Square 类型的对象有一个包含 1 个指向 Square 的指针的数组。 (我相信您应该使用 Shape 而不是 Square。)您必须分配一个 Square 并将其放入 arraySquare(规则 b)。因为你有一个指针,你应该在构造函数中将它设置为 NULL(规则 a),你必须在析构函数中删除它(规则 c)。

然后在setXY()中你想设置4对X&Y,但是arraySquare只有1个instance的空间,还没有分配。一个简单的修复是修改 arraySquare 的定义,使其具有 4 个指针的数组,而不是只有 1 个。在 for 循环中设置 X 和 Y 之前,如果它仍然为 NULL,则必须分配一个 Square 实例(以允许 setXY() 在同一对象上多次调用)。现在您有 4 个指针,您必须更新构造函数以将所有 4 个指针设置为 NULL,并更新析构函数以删除所有 4 个指针。

main() 中,您还使用了一个指针。我留给您将规则应用于它。

注意:使用 Point 类型会更清楚,因为我们通常说“一个正方形有 4 个角/点”。通过使用 Square 和 arraySquare,Square 的每个角都有自己的“名称”。

关于C++,为什么我使用这种数组存储数据时程序会挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13200153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com