2015-08-29 135 views
-2

/Building.exe中的0x00AB6591未處理的異常:0xC0000005:訪問衝突讀取位置0x00000000。/訪問衝突讀取C++

#pragma warning (disable : 4996) 

#include<iostream> 
#include<string> 
#include<cassert> 
using namespace std; 

class Building 
{ 
private: 
    int height; 
    int area; 
    char*address; 
public: 
    Building(); 
    Building(int, int, char*); 
    ~Building(); 
    Building(const Building &); 
    Building & operator=(const Building&); 

    int getHeight()const; 
    int getArea()const; 
    char* getAddress()const; 
    void print()const; 


    void setHeight(int); 
    void setArea(int); 
    void setAddress(char*); 
}; 


Building::Building() 
{ 
    height = 0; 
    area = 0; 
    address = new char[1]; 
    address = NULL; 
} 


Building::Building(int h, int ar, char* a) 
{ 
    height = h; 
    area = ar; 

    address = new char[strlen(a)+1]; 
    assert(address != NULL); 
    address = NULL; 
} 


Building::~Building() 
{ 
    delete[]address; 
} 



Building::Building(const Building & b) 
{ 
    height = b.height; 

    area = b.area; 

    address = new char[strlen(b.address) + 1]; 
    assert(address != NULL); 
    strcpy(address,b.address); 
} 


Building & Building::operator=(const Building& b) 
{ 
    if (this != &b) 
    { 
     delete[]address; 
     height = b.height; 

     area = b.area; 

     address = new char[strlen(b.address) + 1]; 
     assert(address != NULL); 
     strcpy(address, b.address); 
    } 
    return *this; 
} 

int Building::getHeight()const 
{ 
    return height; 
} 


int Building::getArea()const 
{ 
    return area; 
} 


char* Building::getAddress()const 
{ 
    return address; 
} 


void Building::print()const 
{ 
    cout << "Height = " << getHeight() << endl << "Area = " << getArea() << endl << "Address = " << getAddress() << endl; 
} 


void Building::setHeight(int h) 
{ 
    height = h; 
} 


void Building::setArea(int ar) 
{ 
    area = ar; 
} 


void Building::setAddress(char* adr) 
{ 
    strcpy(address, adr); 
} 
//========================================================================================================== 
class House :public Building 
{ 
private: 
    int floors; 
    char*name; 
public: 
    House(); 
    House(int,int,char*,int=0, char* =" "); 
    ~House(); 
    House(const House&); 
    House& operator=(const House&); 

    int getFloors()const; 
    char* getName()const; 


    void setFloors(int); 
    void setName(char*); 


    void print()const; 
}; 

House::House() 
{ 
    floors = 0; 
    name = new char[1]; 
    assert(name != NULL); 
    name = NULL; 
} 
House::House(int h, int ar, char* adr, int f, char* n) :Building(h, ar, adr) 
{ 
    floors = f; 

    name = new char[strlen(n) + 1]; 
    assert(name != NULL); 
    strcpy(name, n); 
} 


House::~House() 
{ 
    delete[]name; 
} 


House::House(const House& h) :Building(h) 
{ 
    floors = h.floors; 

    name = new char[strlen(h.name) + 1]; 
    assert(name != NULL); 
    strcpy(name, h.name); 
} 

House& House::operator=(const House&h) 
{ 
    if (this != NULL) 
    { 
     Building::operator=(h); 
     delete[]name; 
     floors = h.floors; 

     name = new char[strlen(h.name) + 1]; 
     assert(name != NULL); 
     strcpy(name, h.name); 
    } 
    return*this; 
} 

int House::getFloors()const 
{ 
    return floors; 
} 

char* House::getName()const 
{ 
    return name; 
} 


void House::setFloors(int f) 
{ 
    floors = f; 
} 


void House::setName(char* na) 
{ 
    strcpy(name, na); 
} 


void House::print()const 
{ 
    Building::print(); 
    cout << "Floors: " << getFloors() << endl << "Name: " << getName() << endl; 
} 
//============================================================================================================= 

House getBigger(House m[], int size)// a house with bigger avarage height of a floor 
{ 
    House temp; 
    int max = 0; 
    int index = 0; 
    for (int i = 0; i < size; i++) 
    { 
     int avH = (m[i].getHeight()/m[i].getFloors()); 
     if (avH >= max) 
     { 
      max = avH; 
      index = i; 
     } 
    } 

    return m[index]; 
} 
//============================================================================================================= 
int main() 
{ 
    House h1(16,400,"Bla street",4,"Marion"); 
    h1.print(); 

    House h2; 
    h2.setHouse(); 
    h2.print(); 


    House h3; 
    h3.setHouse(); 
    h3.print(); 

    House arr[] = { h1, h2, h3 }; 

    House h4; 
    h4=getBigger(arr, 3); 
    h4.print(); 


    system("pause"); 
    return 0; 
} 

/*我有問題,我對建設方案和house.It的是簡單的可能是,但我不知道爲什麼trows exception.I認爲我嘗試對用戶輸入錯誤的任何地方。請幫忙。以前感謝!*/

+0

錯誤是您獲得的唯一輸出結果嗎?你在崩潰之前給它提供任何輸入嗎? – Tom

回答

1

Your Building構造函數都將地址設置爲null。如果你試圖在任何地方打印地址,你的代碼會給你那個錯誤。

+0

Thanks.Fix it! :))))) – littleFox