【问题标题】:Iterator errors with lists列表的迭代器错误
【发布时间】:2013-03-21 09:31:58
【问题描述】:

所以这是我的程序。这很不言自明。有一个菜单有 4 个选项。每个选项都提供两个不同子菜单的版本。我正在处理列表和向量,但现在列表给了我错误。我对迭代器做错了吗?我很确定这就是我所有错误的原因。

#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;

template <class T>
void lst(T inglethorp, int a)
{
  list<T> mylist;
  int sel = 10;
  T ins;
  int place;
  list<T>::iterator iter; //is this wrong? 
  //error: expected ';' before 'iter'
  //dependent-name 'std::list::iterator' is parsed as a non-type, but instantiation yields a type


  switch(a)
  {
    case 1:
        cout << "What would you like to load to the front?" << endl;
        cin >> ins;
        mylist.push_front(ins);
        cout << endl << endl;
        break;
    case 2:
        cout << "What would you like to load to the back?" << endl;
        cin >> ins;
        mylist.push_back(ins);
        cout << endl << endl;
        break;
    case 3:
        cout << "Where would you like to insert the value?" << endl;
        cin >> place;
        iter = mylist.begin(); //'iter' was not declared in this scope
        for(int i=0; i < place; i++)
            iter++;
        cout << "What would you like to lod at this point?" << endl;
        cin >> ins;
        mylist.insert(iter, ins);
        cout << endl << endl;
        break;
    case 4:
        cout << "What would you like to search for?" << endl;
        cin >> ins;
        iter = find(mylist.begin(), mylist.end(), ins);
        if(iter==mylist.end())
            cout << ins << " was not founf." << endl << endl;
        else
            cout << ins << " is in the list" << endl << endl;
        break;
    case 5:
        cout << "What would you like to remove?" << endl;
        cin >> ins;
        mylist.remove(ins);
        cout << endl << endl;
        break;
    case 6:
        for(iter = mylist.begin(); iter != mylist.end(); iter++)
            cout << *iter << " " << endl << endl;
        break;
    case 0:
        break;
    default:
        cout << "Enter a valid number between 0 and 6" << endl << endl;
        break;
  }
}

void listsub()
{
    cout << endl << endl << "Linked List Sub-Menu" << endl;
    cout << "+++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
    cout << "1. Insert a value at the front of the list" << endl;
    cout << "2. Insert a value at the back of the list" << endl;
    cout << "3. Insert a value at a given position in the list" << endl;
    cout << "4. Search the list for a value" << endl;
    cout << "5. Delete all instances of a value" << endl;
    cout << "6. Print the list contents" << endl << endl;
    cout << "0. Return to main homework menu" << endl;
    cout << "+++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
}

int main()
{
  int sel = 10;
  int subsel = 10;
  int a = 1;
  double b = 1.1;
  int sel2 = 19;
  while(sel != 0)
  {
    sel = 10;
    subsel = 10;
    cout << endl << endl << "Welcome to the CS222 Homework 7 Menu!" << endl;
    cout << "=================================================" << endl;
    cout << "1. Test the vector STL with integers" << endl; //not done yet
    cout << "2. Test the vector STL with doubles" << endl;  //not done yet
    cout << "3. Test the list STL with integers" << endl;
    cout << "4. Test the list STL with doubles" << endl << endl;
    cout << "0. Exit" << endl;
    cout << "=================================================" << endl << endl;

    cin >> sel;

    switch(sel)
    {
        case 1:
            break;
        case 2:
            break;
        case 3:
            while(sel2 != 0)
            {
                listsub();
                cin >> sel2;
                lst(a, sel2);
            }
            sel2 = 12;
            break;
        case 4:
            while(sel2 != 0)
            {
                listsub();
                cin >> sel2;
                lst(b, sel2);
            }
            sel2 = 12;
            break;
        case 0:
            break;
        default:
            cout << "Select 0-4" << endl;
            break;
    }
  }
  return 0;
}

【问题讨论】:

  • 你遇到了什么错误?
  • 你遇到了什么错误?
  • hw7.cpp:在函数'void lst(T,int)'中:hw7.cpp:15:错误:预期';'在'iter' hw7.cpp:34 之前:错误:'iter' 未在此范围内声明 hw7.cpp:在函数'void lst(T, int) [with T = int]'中:hw7.cpp:133:实例化从这里 hw7.cpp:15: error:dependent-name 'std::list::iterator' 被解析为非类型,但实例化产生类型
  • hw7.cpp:15:注意:如果类型是指 hw7.cpp,请说 'typename std::list::iterator':在函数 'void lst(T, int) [with T = double]': hw7.cpp:142: 从这里实例化 hw7.cpp:15: error:dependent-name 'std::list::iterator' 被解析为非类型,但实例化产生一个类型 hw7.cpp: 15:注意:如果是指类型,请说 'typename std::list::iterator'
  • @musicmanz93:好吧,note 已经为您提供了解决方案,但如果您想要的话,我会在回答中添加一些解释。

标签: c++ list stl iterator


【解决方案1】:

您在list&lt;T&gt;::iterator iter 中缺少typename,它应该是:

typename list<T>::iterator iter;

这是因为list&lt;T&gt; 依赖于模板参数T,并且编译器默认假定list&lt;T&gt;::iterator 或任何其他依赖于T 的符号是一个值而不是一个类型。使用typename 会告知编译器它确实是一个类型而不是一个值。

更多信息,谷歌“依赖名称查找”(例如wiki)。基本上问题是你可以专门化,比如class list&lt;int&gt; 并将iterator 定义为一个值,并且在第一个编译过程(解析)T 没有实例化(没有值)并且编译器还不知道几乎所有可能的专业。

【讨论】:

    【解决方案2】:

    发个typename

    typename typename list<T>::iterator iter;
    

    【讨论】:

      【解决方案3】:

      您需要typename 关键字才能使其正常工作。但试图猜测你在做什么,我想你会想让mylist静态。您的代码始终使用新的空列表,可能不是您想要的。

      template <class T>
      void lst(T inglethorp, int a)
      {
        static list<T> mylist;
        int sel = 10;
        T ins;
        int place;
        typename list<T>::iterator iter; //OK
      

      ...

      【讨论】:

        猜你喜欢
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        相关资源
        最近更新 更多