2013-03-21 98 views
1

所以這是我的程序。這很自我解釋。有一個菜單有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; 
} 
+3

你得到什麼錯誤? – Jace 2013-03-21 09:33:47

+2

你有什麼錯誤? – Nick 2013-03-21 09:34:32

+0

hw7.cpp:函數'void lst(T,int)': hw7.cpp:15:error:expected';' 'iter'之前 hw7.cpp:34:錯誤:'iter'未在此範圍內聲明 hw7.cpp:函數'void lst(T,int)[with T = int]': hw7.cpp: 133:從這裏實例化 hw7.cpp:15:錯誤:依賴名'std :: list :: iterator'被解析爲非類型,但實例化產生類型 – musicmanz93 2013-03-21 09:37:22

回答

4

你缺少一個typenamelist<T>::iterator iter這應該是:

typename list<T>::iterator iter; 

這是因爲list<T>取決於模板參數T,編譯器假設默認是list<T>::iterator或任何其他符號依賴T是一個值,而不是一個類型。使用typename通知編譯器它確實是一個類型而不是一個值。

欲瞭解更多信息,谷歌「依賴名稱查找」(例如wiki)。基本上問題是你可以專門化,比如說class list<int>並將iterator定義爲一個值,並且在第一次編譯階段(解析)T沒有被實例化(沒有值),編譯器還不知道每個可能的特化。

+0

...它工作。非常感謝你:) – musicmanz93 2013-03-21 09:42:58

+0

@ musicmanz93:很高興我能幫忙;) – Antoine 2013-03-21 09:47:26

0

將一個typename

typename typename list<T>::iterator iter; 
0

您需要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 

...