【问题标题】:List iterator outside range列表迭代器超出范围
【发布时间】:2011-05-24 21:16:42
【问题描述】:

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;

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

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;

};

【问题讨论】:

  • 如果不查看有问题的代码部分,就很难判断您的程序出了什么问题。到目前为止,您提供的内容远非理想,但似乎不是问题。
  • 您的问题不是由于此代码。尝试创建一个最小的、完整的程序来展示这种行为。我怀疑当你将你的真实程序缩减为一个微不足道的样本时,你会发现问题。有关详细信息,请参阅sscce.org
  • 附言。如果temp 不是列出的单词,则此函数可能会调用未定义的行为。将c 初始化为有效值或空指针。
  • 我同意 Rob 的观点,不初始化 c 可能是问题的一部分
  • 该死,我希望我现在的 id 是 Robbb。

标签: c++ list stl


【解决方案1】:

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

【讨论】:

  • 第一次使用列表时出现错误。在此之前没有对其应用其他操作(声明本身除外)。
  • 你死定了!我重新检查了我的代码,似乎我在我的列表中应用了一个contactList.clear()。这就是问题所在。但问题仍然存在。为什么使用 .clear() 会出现这样的错误?
  • 好的,最终想通了。 clear 的问题是它删除了指针,而不是用 new 创建的实际对象。对于那些我(你)必须手动删除它们。谢谢!
  • @AdrianMar:我不确定这种解释有什么意义——清除列表会泄漏内存,但这不应该破坏实际的列表对象。但如果你在工作,我想生活中还有更可怕的谜团。
猜你喜欢
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 2018-01-30
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多