列表的迭代器错误
Iterator errors with lists
这就是我的程序。这是不言自明的。有一个菜单有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;
}
您在list<T>::iterator iter
中缺少一个typename
,它应该是:
typename list<T>::iterator iter;
这是因为list<T>
依赖于模板参数T
,并且编译器默认认为list<T>::iterator
或依赖于T
的任何其他符号是一个值而不是类型。使用typename
通知编译器它确实是一个类型而不是一个值。
更多信息,谷歌"依赖名称查找"(例如wiki)。基本上,问题是您可以专门化,例如class list<int>
并将iterator
定义为一个值,并且在第一次编译过程(解析)中T
没有实例化(没有值),并且编译器还不知道每个可能的专门化。
添加typename
typename typename list<T>::iterator iter;
您需要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
…
相关文章:
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 初始化迭代器错误 C++ 在 GCC 编译器中
- 迭代器类的重载前缀增量运算符会引发分段错误
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 在静态库中使用输出迭代器时出现链接器错误
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- C++结构编译器错误询问向量的迭代器
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 编译错误 std::vector<std::shared_ptr<T>>迭代器和擦除方法
- 赛松迭代器错误
- 向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量
- C++,弹出调试断言失败窗口,我得到矢量迭代器不兼容的错误运行时
- 在 leveldb 的 c++ 示例中声明迭代器时出现分段错误
- std::map 擦除 - 将迭代器传递给错误的映射
- C++向量迭代器nth_element编译错误
- 如何修复错误,迭代器未在此范围内声明,并且迭代器未命名类型'
- 如何修复使用矢量和迭代器打印值的错误
- C++ 迭代器错误"does not refer to a value"
- 使用 -std=c++0x 会导致错误:“迭代器”未命名类型错误