musicmanz93
musicmanz93

Reputation: 29

Iterator errors with lists

So this is my program. It's pretty self explanatory. There is one menu that has 4 options. Each option lets out to a version of two different sub-menus. I'm dealing with lists and vectors, but right now the lists are giving me errors. Am I doing something wrong with the iterators? I'm pretty sure that's what all of my errors are with.

#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;
}

Upvotes: 1

Views: 1151

Answers (3)

qPCR4vir
qPCR4vir

Reputation: 3571

You need the typename keyword to make it work rigth. But trying to gess what you are doing, I think you will want to make mylist static. Your code work always with a new empty list, with probably is not what you want.

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

...

Upvotes: 0

Antoine
Antoine

Reputation: 14064

You are missing a typename in list<T>::iterator iter which should be:

typename list<T>::iterator iter;

This is because list<T> depends on the template parameter T, and the compiler assumes by default that list<T>::iterator or any other symbol dependant on T is a value and not a type. Using typename informs the compiler that it really is a type and not a value.

For more info, google "dependent name lookup" (e.g. wiki). Basically the problem is that you could specialize, say class list<int> and define iterator as a value, and in the first compiling pass (parsing) T is not instantiated (has no value) and the compiler doesn't know yet about every possible specialization.

Upvotes: 4

masoud
masoud

Reputation: 56479

Put a typename

typename typename list<T>::iterator iter;

Upvotes: 0

Related Questions