gpt4 book ai didi

c++ - 列表迭代器超出范围

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:33 28 4
gpt4 key购买 nike

我对 STL list class 有疑问.我有一个名为 Contact 的基类和三个派生类 Friend、Colleague 和 Acquaintance。派生类的每个实例都有我在 fill*Class*Details() 函数中修改的某些字段。问题是当它到达 push_back 行时,我的程序给我一个错误提示 list insert iterator outside range .那是什么?

void Agenda::pushContact(string line, string temp)//function that adds a contact of type temp, containing the fields from line to the list
{
Contact *c;

if(temp=="friend") //the contact to add is of type friend
{
c = new Friend();
fillFriendDetails(c,line);//the details of Friend c will be filled
}
else if(temp=="colleague")
{
c = new Colleague();
fillColleagueDetails(c,line);//the details of Colleague c will be filled
}
else if(temp=="acquaintance")
{
c = new Acquaintance();
fillAcquaintanceDetails(c,line);//the details of Acquaintance c will be filled
}
contactList.push_back(c);
}

此外,contactList 定义为 list <Contact*> contactList; .

编辑:这是 Contact 类(+派生类)的定义方式:

class Contact
{
public:
string getFullName() { string fullName;fullName.append(firstName); fullName.append(" ");fullName.append(lastName); return fullName;}
public:
void setFullName(string newFirstName, string newLastName) { firstName = newFirstName; lastName = newLastName;}
public:
string getFirstName() { return firstName;}
public:
void setFirstName(string newFirstName) {firstName = newFirstName;}
public:
string getLastName(){return lastName;}
public:
void setLastName(string newLastName){lastName = newLastName;}
public:
string getPhoneNo(){return phoneNo;}
public:
void setPhoneNo(string newPhoneNo) {phoneNo = newPhoneNo;}
public:
void setType(string newType){type=newType;}
public:
string getType(){return type;}
private:
string firstName;
string lastName;
string phoneNo;
string type;

//SubClass setters and getters
public:
virtual void setDateOfBirth(string birth) {}
virtual string getDateOfBirth() {return 0;}
virtual void setCity (string newCity) {}
virtual string getCity() {return 0;}
virtual void setFaculty (string newFaculty) {}
virtual string getFaculty() {return 0;}
virtual void setGroup (string newGroup) {}
virtual string getGroup() {return 0;}
virtual void setJob (string newJob) {}
virtual string getJob () {return 0;}


};


class Friend : public Contact
{
public:
void setDateOfBirth(string birth) {dateOfBirth=birth;}
public:
string getDateOfBirth() {return dateOfBirth;}
public:
void setCity (string newCity){city=newCity;}
public:
string getCity(){return city;}

private:
string dateOfBirth;
string city; //current city of residence
};

class Colleague : public Contact
{
public:
void setFaculty (string newFaculty){faculty = newFaculty;}
public:
string getFaculty(){return faculty;}
public:
void setGroup (string newGroup){group = newGroup;}
public:
string getGroup(){return group;}


private:
string faculty;
string group;
};

class Acquaintance : public Contact
{
public:
void setJob (string newJob){job=newJob;}
public:
string getJob (){return job;}

private:
string job;

};

最佳答案

看起来列表在代码中的不同位置管理不善,因为错误暗示 end 不正确。很可能是列表被删除/超出范围,或者对列表元素执行了一些不正确的 erase(比如使用无效的迭代器)。

关于c++ - 列表迭代器超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116998/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com