使用 ARM GCC 编译列表迭代器时的模板编译时错误

Posted

技术标签:

【中文标题】使用 ARM GCC 编译列表迭代器时的模板编译时错误【英文标题】:Template compile-time errors when compiling list iterators with ARM GCC 【发布时间】:2011-07-10 06:18:21 【问题描述】:

在 GCC ARM 下编译时,这段代码让我很头疼。我在 MSVC++ 编译器 2010 中使用它很好。我得到如下编译错误:

错误 1 ​​错误:预期为 ';'在“我”之前 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

【问题讨论】:

不错的代码。你有什么问题? 大家好,刚刚更新了我的问题 :) 我真的很困惑,因为这是工作代码,将其移植到 GCC/跨平台。 【参考方案1】:

它不会编译,因为它充满了错误:

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 没有抱怨?

【讨论】:

啊,太棒了,我不知道 -Wall,哇,你说得对,有很多模棱两可的地方。好的,我将尝试进行这些更正,并让您知道进展如何。谢谢 太好了,开始工作了。无论何时我在使用 E 定义的类型上使用 ::iterator,我都需要通过在该声明之前插入“typename”来告诉编译器它是一个类型,因为它不知道并且显然不想假设。 @Ryan:帮助拯救世界(或至少 ***)编辑您的问题以结合您所学的内容。您还可以为自己的问题创建一个答案并接受它(自我回答对 SO 有好处)。

以上是关于使用 ARM GCC 编译列表迭代器时的模板编译时错误的主要内容,如果未能解决你的问题,请参考以下文章

gcc 常用编译选项

arm-linux-gcc 常用参数讲解 gcc编译器使用方法

为啥在编译 Linux 内核和 uBoot 时使用 arm-linux-gnueabi-gcc 而不是 arm-none-eabi-gcc?

arm-linux-gcc常用参数讲解 gcc编译器使用方法

用Linux编译程序时用到eXosip这个库,下载安装后,gcc编译程序能通过,arm-linux-gcc则不行,该怎么做?

arm gcc 默认编译它的 libc