函数退出后丢失的对象

时间:2015-10-27 02:39:18

标签: c++ class linked-list

我遇到类数据和链表的问题,在使用textfile参数调用构造函数后,head指向null。在尝试调试之后,没有任何析构函数被调用,因此我在链接列表中删除对象时丢失了。

中的

代码

char fileName[] = "data.txt";
    queue housesOnList(fileName);
    data house;

    stack houseInterested;
    char response;

    cout << "\nthe houses we will visit today:" << endl << endl;
    housesOnList.display ();

类函数

queue::queue(const char * textfile)
{
    data house;
    queue houses;
    char * sqFoot = new char[1];
    char * bedR = new char[1];
    char * bathR = new char[1];
    string addr;
    string description;
    ifstream myfile(textfile);
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            getline(myfile, addr);
            getline(myfile, description);
            myfile >> sqFoot >> bedR >> bathR;
            house.setAddress(addr.c_str());
            house.setDiscription(description.c_str());
            house.setSqFoot(atoi(sqFoot));
            house.setBedrooms(atoi(bedR));
            house.setBathroom(atof(bathR));
            houses.enqueue(house);
        }
    }
}
void queue::enqueue(data & item)
{
    Node * n = new Node(item);
    /*if (head != NULL)
    {

        n->next = head;
        head = n;

        return;
    }
    else
    {
        head = n;
        return;
    }*/
    n->next = NULL;
    if (head != NULL)
    {
        curr = head;
        while (curr->next != NULL)
        {
            curr = curr->next;
        }
        curr->next = n;
    }
    else
    {
        head = n;
    }

}

对象类

data::data()
{
    this->Address = NULL;
    this->Description = NULL;
    Bedr = 0;
    Bathr = 0;
    sqFoot = 0;
}
data::data(const data & item)
{
    *this = item;
}
/*data::~data()
{
if (this->Address)
{
delete[] this->Address;
}
}*/
const char* data::getAddress()
{
    return this->Address;
}
const char *data::getDescription()
{
    return this->Description;
}
int data::getBedrooms() const
{
    return Bedr;
}
int data::getSqFoot() const
{
    return sqFoot;
}
float data::getBathroom() const
{
    return Bathr;
}
void data::setAddress(const char * addr)
{
    if (this->Address)
    {
        delete[] this->Address;
    }
    this->Address = new char[strlen(addr) + 1];
    strcpy(this->Address, addr);
}
void data::setDiscription(const char * desc)
{
    if (this->Description)
    {
        delete[] this->Description;
    }
    this->Description = new char[strlen(desc) + 1];
    strcpy(this->Description, desc);
}
bool data::setBathroom(const float bathR)
{
    if (bathR < MIN)
    {
        return false;
    }
    this->Bathr = bathR;
    return true;
}
bool data::setBedrooms(const int bedR)
{
    if (bedR < MIN)
    {
        return false;
    }
    this->Bedr = bedR;
    return true;
}
bool data::setSqFoot(const int sqFoot)
{
    if (sqFoot < MIN)
    {
        return false;
    }
    this->sqFoot = sqFoot;
    return true;
}
ostream& operator<< (ostream& out, const data& house)
{
    out << house.Address << "\t" << house.Description << "\t" << house.sqFoot << "\t" << house.Bedr << "\t" << house.Bathr << "\t" << endl;
    //item.print();
    return out;
}
void data::operator= (const data& s)
{
    if (this == &s)
        return;
    this->sqFoot = s.sqFoot;
    //this->id = s.id;
    setAddress(s.Address);
    setDiscription(s.Description);
    this->Bedr = s.Bedr;
    this->Bathr = s.Bathr;
}

先谢谢!

2 个答案:

答案 0 :(得分:0)

您需要提供“数据”的定义,这可能是一个头文件。

答案 1 :(得分:0)

queue构造函数内部OP创建另一个queue并将读取的数据添加到它而不是正在构建的queue

queue::queue(const char * textfile)
{
    data house;
    queue houses; <<-- local variable queue within queue
    ...
    ifstream myfile(textfile);
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            ...
            houses.enqueue(house); <<-- data goes into houses, not this
        }
    }
} <<-- houses goes out of scope and is destroyed

从文件中读取的所有内容都进入了局部变量houses,这个变量在构造函数结束时会被大量丢失的delete内存(应该修复它)。