Access violation reading c++

-3

/Unhandled exception at 0x00AB6591 in Building.exe: 0xC0000005: Access violation reading location 0x00000000./

#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;
}

I have problem with my program for building and a house. It is simple maybe but I don't know why throws exception. I think that my try for user input is wrong anywhere.

c++
access-violation
asked on Stack Overflow Aug 29, 2015 by littleFox • edited Dec 19, 2019 by halfer

1 Answer

1

Your Building constructors both set address to null. If you are trying to print the address anywhere, your code will give you that error.

answered on Stack Overflow Aug 29, 2015 by Tom

User contributions licensed under CC BY-SA 3.0