【发布时间】: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 >> sqFoot结合是个坏主意。 -
while (!myfile.eof())几乎从不工作。在这里阅读:stackoverflow.com/questions/5605125/… -
而不是一个有十几个setter方法的数据类,考虑一个更完整的构造函数实现。
-
队列构造函数可能不应该读取文件。最好设置队列并将房屋数据读取到房屋或房屋工厂函数。
标签: c++ class linked-list