0

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を入力するたびにハングします。誰でも助けることができますか?

4

1 に答える 1

1

未割り当てのオブジェクトにアクセスするため、プログラムがハングします。ポインタ宣言を表示するたびに、次の3つのルールを適用する必要があります。a)デフォルト値に設定されていますか?b)割り当て/割り当てられていますか?c)削除されていますか?

クラスSquareで、次のデータメンバーを宣言しました。

Square *arraySquare[1];

したがって、Square型のオブジェクトには、Squareへの1つのポインターの配列があります。(私はあなたが正方形の代わりに形を持っているべきだと信じています。)あなたは正方形を割り当ててそれを入れる必要がありますarraySquare(ルールb)。ポインタがあるのでNULL、コンストラクタで(ルールa)に設定し、デストラクタ(ルールc)で削除する必要があります。

次に、setXY()X&Yの4つのペアを設定しますがarraySquare、1つのインスタンス用のスペースしかなく、割り当てられていません。簡単な修正の1つは、arraySquareの定義を変更して、1つだけではなく4つのポインターの配列を作成することです。forループでXとYを設定する前に、SquareインスタンスがまだNULLの場合は、Squareインスタンスを割り当てる必要がありますsetXY()(同じオブジェクトで複数回呼び出されます)。ポインタが4つあるので、コンストラクタを更新して4つすべてをNULLに設定し、デストラクタを更新して4つすべてを削除する必要があります。

ではmain()、ポインタも使用します。ルールを適用するのはあなたに任せます。

注意:通常、「正方形には4つのコーナー/ポイントがある」と言うので、タイプポイントを使用する方が明確です。また、Square anを使用すると、SquareのarraySquare各コーナーに独自の「名前」が付けられます。

于 2012-11-02T23:43:43.393 に答える