2012-11-02 37 views
0

當我試圖輸入x pt.1的任何座標時,我的程序只是簡單地掛起。C++,當我使用這種數組來存儲數據時,爲什麼我的程序掛起?

任何人都可以幫助我嗎?

// 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

變量'square'是一個未初始化的'Square *'並被解除引用,導致未定義的行爲。 – hmjd

+0

另外你還要經過'arraySquare'這個未定義的行爲。 – Dan

+1

嘗試使用調試器來查看會發生什麼。可能不是因爲數組,而是該cin處於錯誤狀態。 – Axel

回答

1

程序掛起,因爲它訪問未分配的對象。每次看到指針聲明時,都必須應用3條規則:a)是否將其設置爲默認值? b)是否分配/分配? c)是否被刪除?

在類廣場,聲明瞭該數據成員:

Square *arraySquare[1]; 

所以,類型廣場的目的有1個指針的陣列以一個正方形。 (我相信你應該有形狀而不是方形。)你必須分配一個Square並將其放在arraySquare(規則b)中。既然你有一個指針,你應該在構造函數中將它設置爲NULL(規則a),並且你必須在析構函數中刪除它(規則c)。

然後在setXY()中,您想要設置4對X & Y,但arraySquare只有1個實例的空間並且尚未分配。一個簡單的解決方法是修改arraySquare的定義,使其具有4個指針的數組,而不是1個。在for循環中設置X & Y之前,必須先分配Square實例(如果它仍爲NULL)(以允許setXY()在同一個對象上被多次調用)。現在你有4個指針,你必須更新構造函數以將所有4設置爲NULL,並更新析構函數以刪除全部4.

main()中,還使用了一個指針。我把它留給你來應用規則。

注意:使用類型Point會更清晰,因爲我們通常會說「一個正方形有四個角/點」。並且通過使用Square一個arraySquare,Square的每個角落都有自己的「名字」。

+0

我還有一個問題,我怎樣在Assn2中調用setXY()?我相信Square * square;然後調用square-> setXY()也掛起我的程序。我該如何解決這個問題? –

+0

Assn2?你的意思是main()?你是否將3條規則應用到了這個方形指針? – PRouleau

+0

是的主要。我真的不知道如何開始。我試圖做的每一件事,改變,它只是簡單地掛起程序。猜猜我會放棄這項任務,只是失敗了。 –

相關問題