0

Im having problems with class data and linked list, after the constructor is called with the textfile parameter head points to null. after attempting to debug it none of the destructors were called so i am lost in where its deleting the objects from the linked list.

code in main

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

    stack houseInterested;
    char response;

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

class functions

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

}

object class

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

Thanks in advanced !

samound
  • 9
  • 2
  • Recommend ditching the char arrays for strings. Make your life a lot easier. – user4581301 Oct 27 '15 at 03:10
  • `char * sqFoot = new char[1];` combined with `myfile >> sqFoot` is a bad idea. – user4581301 Oct 27 '15 at 03:11
  • `while (!myfile.eof())` almost never works. Read here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Oct 27 '15 at 03:12
  • rather than a data class with a dozen setter methods, consider a more complete constructor implementation. – user4581301 Oct 27 '15 at 03:16
  • The queue constructor probably shouldn't be reading the file in. Its better off setting up a queue and leaving the house data reading to the house or a house factory function. – user4581301 Oct 27 '15 at 03:19

2 Answers2

0

You need to supply the definition of "data" which is probably a header file.

doug
  • 1,698
  • 1
  • 8
  • 16
  • This is not an answer but is a comment and therefore belongs in the "comments" section. Please move it there before getting down-voted – YePhIcK Oct 27 '15 at 02:56
  • How do I change it to a comment? – doug Oct 27 '15 at 03:22
  • You can't at this point. You need more reputation. Sucks, but it keeps the spam down. – user4581301 Oct 27 '15 at 03:25
  • 1
    Ah yes, the spam problem. Well, I was trying to be helpful to the OP and didn't see anywhere else to leave it. Perhaps someone with sufficient privilege can edit or move it. Well, I finished registering. – doug Oct 27 '15 at 03:41
0

Inside a queue constructor OP creates another queue and adds the read data to it instead of the queue under construction:

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

Everything read from the file went into local variable houses which is destroyed with vast loss of undeleted memory (Should fix that, by the way) at the end of the constructor.

user4581301
  • 29,019
  • 5
  • 26
  • 45