使用ARM GCC编译列表迭代器时的模板编译时错误
Template compile-time errors when compiling list iterators with ARM GCC
这段代码在GCC ARM下编译时让我很头疼。我使用它很好与msvc++编译器2010。我得到如下编译错误:
错误1错误:期望';'在'i'之前C:UsersRyanDesktopdropletssourceMultiList.h 62
为什么我的模板代码不能使用GCC编译?
#ifndef MULTILIST_H
#define MULTILIST_H
#include <list>
#include <fstream>
using namespace std;
/*
A list of lists
*/
template <typename E>
class MultiList {
protected:
list<list<E>*> m_lists;
list<E> *m_pCurrList;
public:
MultiList();
~MultiList();
/*
Starts a new list internally, given the first element
*/
void BeginNewList(E firstElement);
/*
Adds an element to the current list
*/
void AddElement(E newElement);
/*
Removes a given element from it's place in one of the lists,
splitting that list into two lists internally.
*/
void RemoveElement(E element);
/*
Returns a list of all element lists
*/
list<list<E>*> *GetLists() {
return &m_lists;
};
/*
Return the list that's currently being populated with AddElement()
*/
list<E>* GetCurrentList() {
return m_pCurrList;
};
};
template<typename E>
MultiList<E>::MultiList() {
m_pCurrList = NULL;
}
template<typename E>
MultiList<E>::~MultiList() {
for(list<list<E>*>::iterator i = m_lists.begin(); i != m_lists.end(); i++) {
list<E>::iterator j;
for(j = (*i)->begin(); j != (*i)->end(); j++) {
SDELETE(*j)
}
SDELETE(*i)
}
}
/*
Starts a new list internally, given the first element
*/
template<typename E>
void MultiList<E>::BeginNewList(E firstElement) {
list<E> *newlist = new(list<E>);
newlist->push_back(firstElement);
m_lists.push_back(newlist);
m_pCurrList = newlist;
}
/*
Adds an element to the current list
*/
template<typename E>
void MultiList<E>::AddElement(E newElement) {
m_pCurrList->push_back(newElement);
}
/*
Removes a given element from it's place in one of the lists,
splitting that list into two lists internally.
*/
template<typename E>
void MultiList<E>::RemoveElement(E element) {
list<E>* found = NULL;
list<E>::iterator foundIT = NULL;
// find which list 'element' is in
for(list<list<E>*>::iterator i = m_lists.begin(); i != m_lists.end(); i++) {
list<E>::iterator j;
for(j = (*i)->begin(); j != (*i)->end(); j++) {
E listElement = (*j);
if(listElement == element) {
found = (*i);
foundIT = j;
break;
}
}
if (j != (*i)->end()) break; // we breaked out of the inner loop
}
// now erase it and split the list
if (found) {
list<E>::iterator next = found->erase(foundIT);
list<E> *newlist = new(list<E>);
m_lists.push_back(newlist);
newlist->splice(newlist->begin(), *found, next, found->end());
SDELETE(element)
}
}
#endif
无法编译,因为它被'errors '填充了:
g++ -Wall /tmp/junk.c
/tmp/junk.c: In destructor ‘MultiList<E>::~MultiList()’:
/tmp/junk.c:62:9: error: need ‘typename’ before ‘std::list<std::list<E>*>::iterator’ because ‘std::list<std::list<E>*>’ is a dependent scope
/tmp/junk.c:62:34: error: expected ‘;’ before ‘i’
/tmp/junk.c:62:55: error: ‘i’ was not declared in this scope
/tmp/junk.c:63:9: error: need ‘typename’ before ‘std::list<E>::iterator’ because ‘std::list<E>’ is a dependent scope
/tmp/junk.c:63:27: error: expected ‘;’ before ‘j’
/tmp/junk.c:64:13: error: ‘j’ was not declared in this scope
/tmp/junk.c:65:23: error: there are no arguments to ‘SDELETE’ that depend on a template parameter, so a declaration of ‘SDELETE’ must be available
/tmp/junk.c:65:23: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
/tmp/junk.c:66:9: error: expected ‘;’ before ‘}’ token
/tmp/junk.c:67:19: error: there are no arguments to ‘SDELETE’ that depend on a template parameter, so a declaration of ‘SDELETE’ must be available
/tmp/junk.c:68:5: error: expected ‘;’ before ‘}’ token
/tmp/junk.c: In member function ‘void MultiList<E>::RemoveElement(E)’:
/tmp/junk.c:97:5: error: need ‘typename’ before ‘std::list<E>::iterator’ because ‘std::list<E>’ is a dependent scope
/tmp/junk.c:97:23: error: expected ‘;’ before ‘foundIT’
/tmp/junk.c:100:9: error: need ‘typename’ before ‘std::list<std::list<E>*>::iterator’ because ‘std::list<std::list<E>*>’ is a dependent scope
/tmp/junk.c:100:34: error: expected ‘;’ before ‘i’
/tmp/junk.c:100:55: error: ‘i’ was not declared in this scope
/tmp/junk.c:101:9: error: need ‘typename’ before ‘std::list<E>::iterator’ because ‘std::list<E>’ is a dependent scope
/tmp/junk.c:101:27: error: expected ‘;’ before ‘j’
/tmp/junk.c:102:13: error: ‘j’ was not declared in this scope
/tmp/junk.c:106:17: error: ‘foundIT’ was not declared in this scope
/tmp/junk.c:114:9: error: need ‘typename’ before ‘std::list<E>::iterator’ because ‘std::list<E>’ is a dependent scope
/tmp/junk.c:114:27: error: expected ‘;’ before ‘next’
/tmp/junk.c:117:51: error: ‘next’ was not declared in this scope
/tmp/junk.c:119:5: error: expected ‘;’ before ‘}’ token
使用-Wall
并了解它的缺点是什么。一个更好的问题可能是为什么MSVC 没有投诉?
相关文章:
- 在静态库中使用输出迭代器时出现链接器错误
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- C++:在进行切片时对迭代器的约定,特别是对于访问最后一个元素并最终将其删除
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 编译错误 std::vector<std::shared_ptr<T>>迭代器和擦除方法
- std::vector::erase() 真的会在擦除时使迭代器失效吗?
- 插入新元素时结束迭代器会获得更新
- 当迭代器添加到添加到列表向量的结构时,迭代器停止指向值?
- C++:使用clang编译以迭代器为值的映射时出现巨大错误
- 单元测试时操作迭代器
- C++算法在条件为真时推进迭代器
- std::vector::erase()是否在擦除时使迭代器无效
- 使用 -std=c++0x 会导致错误:“迭代器”未命名类型错误
- 试图从容器获取迭代器时出现编译错误
- 是否有可能在运行时检查迭代器是否没有越过end()迭代器
- 正在编译具有相同迭代器的嵌套循环,为什么会这样?
- 使用基于范围的for循环时需要迭代器
- 使用ARM GCC编译列表迭代器时的模板编译时错误
- 从矢量中擦除时矢量迭代器不兼容
- 在自包含向量循环中删除时"矢量迭代器不兼容"