2011-07-10 7 views
0

このコードは、GCC ARMでコンパイルすると多くの頭痛を与えます。私はそれをMSVC++コンパイラ2010でうまく使っています。コンパイルエラーは次のようになります。GCCでリストイテレータをコンパイルするときのテンプレートコンパイル時エラーGCC

エラー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 
+0

ニースコード。あなたの質問は何ですか? – Flimzy

+0

エラーを投稿する! –

+0

こんにちは、ちょうど私の質問で更新されました:)これはGCC /クロスプラットフォームに移植するコードを作業しているので、私は本当に混乱しています。 – Ryan

答えて

1

それがぎっしりいっぱいo'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 ではないのですか?

+0

ああ、私は知りませんでした - ウォール、あなたは間違いがたくさんあります。さて、私はこれらの訂正をして、それがどのようになっているかを知らせるために刺すようにします。ありがとう – Ryan

+0

素晴らしい、それは働いている。 Eを使って定義された型のイテレータをどこに使っても、コンパイラには型宣言の前に 'typename'を挿入して型を宣言する必要がありました。 – Ryan

+2

@Ryan:世界を救う(または少なくともStackOverflow)あなたの質問を編集して、あなたが学んだ内容を組み込むことができます。自分の質問に対する回答を作成し、その代わりに受け入れることもできます(自己回答はSOに適しています)。 – msw

関連する問題