无法取消引用结束列表迭代器

问题描述 投票:0回答:1

我目前正在从事有关浏览器历史记录的项目。我正在使用STL的列表实现来跟踪不同的网站。我已经弄清楚了大部分,但似乎无法解决错误:

截屏;无法取消引用终止列表迭代器。

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9ERUJRVi5wbmcifQ==” alt =“;不能取消引用结束列表迭代器。

网站数据包含在我的网站对象中。

class BrowserHistory {
   private:
      list<Site> history;
      list<Site>::iterator current = history.begin();
   public:
      void visitSite(string, size_t)
      void backButton();
      void forwardButton();
      void readFile(string);
};

void BrowserHistory::visitSite(string x, size_t y)
{
   while (current != history.end()) {
       history.pop_back();
   }
   history.push_back({ x, y });
   current++;
}

void BrowserHistory::backButton()
{
   if (current != history.begin())
       current--;
}

void BrowserHistory::forwardButton()
{
   if (current != history.end())
       current++;
}

void BrowserHistory::readFile(string filename)
{
   string action, url;
   size_t pageSize;
   ifstream dataIn;
   dataIn.open(filename);

   while (!dataIn.eof()) {
       dataIn >> action;
       if (action == "visit") {
           dataIn >> url >> pageSize;
           history.push_back({ url, pageSize });
           current++;
       }
       if (action == "back") {
           current--;
       }
       if (action == "forward") {
           current++;
       }
   }
   dataIn.close();
}

有人可以向我解释什么地方吗?预先感谢您的帮助。

我目前正在从事有关浏览器历史记录的项目。我正在使用STL的列表实现来跟踪不同的网站。我大部分都想通了,但是我似乎无法解决...

c++ linked-list doubly-linked-list
1个回答
1
投票

初始化current时,列表为空。即使将元素添加到列表中也不会突然使它成为有效的迭代器,即使它与end不同。

© www.soinside.com 2019 - 2024. All rights reserved.