使用 ARM GCC 编译列表迭代器时出现模板编译时错误
这段代码在GCC ARM下编译时让我很头疼。我在 MSVC++ 编译器 2010 中很好地使用它。我收到如下编译错误:
Error 1 error : Expected ';' before 'i' C:\Users\Ryan\Desktop\droplets\source\MultiList.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
This code gives me many headaches when compiling under GCC ARM. I am using it fine with MSVC++ compiler 2010. I get compile errors like:
Error 1 error : expected ';' before 'i' C:\Users\Ryan\Desktop\droplets\source\MultiList.h 62
Why won't my templated code compile using 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不会编译,因为它充满了错误:
使用
-Wall
并了解它的抱怨是什么。更好的问题可能是为什么 MSVC 不抱怨?It won't compile because it is chock full o'errors:
Use
-Wall
and understand what its complaints are. A better question might be why did MSVC not complain?