【问题标题】:objects getting lost after function exits函数退出后对象丢失
【发布时间】:2015-10-27 02:39:17
【问题描述】:

在使用 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;
}

提前致谢!

【问题讨论】:

  • 建议为字符串放弃 char 数组。让您的生活更轻松。
  • char * sqFoot = new char[1];myfile &gt;&gt; sqFoot 结合是个坏主意。
  • while (!myfile.eof()) 几乎从不工作。在这里阅读:stackoverflow.com/questions/5605125/…
  • 而不是一个有十几个setter方法的数据类,考虑一个更完整的构造函数实现。
  • 队列构造函数可能不应该读取文件。最好设置队列并将房屋数据读取到房屋或房屋工厂函数。

标签: c++ class linked-list


【解决方案1】:

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

【讨论】:

  • 这不是答案,而是评论,因此属于“cmets”部分。请在被否决之前将其移到那里
  • 如何将其更改为评论?
  • 你现在不能。你需要更多的声誉。很烂,但它可以减少垃圾邮件。
  • 啊,是的,垃圾邮件问题。好吧,我试图对 OP 有所帮助,但没有看到其他任何地方可以离开它。也许有足够权限的人可以编辑或移动它。嗯,我注册完了。
【解决方案2】:

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,该变量在构造函数结束时会因 undeleted 内存的大量损失而被破坏(顺便说一句,应该解决这个问题)。

【讨论】:

  • 我删除了“排队房屋;”并将houses.enqueue(house) 更改为 enqueue(house)
猜你喜欢
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多