2011-04-09 58 views
1

我試圖用枚舉類型和模板類,但我不知道爲什麼下面的代碼不工作:G ++編譯器無法識別枚舉類型在C++

#include <stdio.h> 
#include <string.h> 

class Multilist { 
    //TYPE DEFINITION 
    struct mlNode;      //forward declarations 
    struct dlNode; 
    template <class T> struct mlCat; 

    typedef char tstr[21];    //our string type 
    enum catIterator {ID=0, OCCUPATION,LOCATION}; 

    struct mlNode { //Multilist Node 
     tstr name; int id; 
     mlNode* p[3]; //next nodes 
     mlNode* n[3]; //previous nodes 
     dlNode* h[3]; //h[i] point to the entry in category i 
     mlNode(tstr sName, int sId) { 
      strcpy(name,sName); id=sId; //nOccupation=snOccupation; nId=snId; nLocation=snLocation; 
     } 
    }; 

    // One class to rule them all =) 
    template <class T> struct mlCat { //Multilist Category 
     catIterator c; 
     mlCat(catIterator tc): head(0), c(tc) {}; 

     struct dlNode { //list node 
      dlNode *next; 
      T data; mlNode *link; //data & link to the record in the db 
      dlNode(T d, mlNode *l, dlNode *n=0): link(l),data(d),next(n) {}; 
     } *head; 

    }; 

    //CATEGORY DEFINITION 
    mlCat<int> catId(ID); 
    mlCat<tstr> catOccupation(OCCUPATION); 
    mlCat<tstr> catLocation(LOCATION); 


}; 

int main(int narg, char * arg[]) { 
    return 0; 
} 

在Eclipse中返回一個錯誤在「類別定義」部分:

../src/multilist.cpp:109: error: ‘ID’ is not a type
../src/multilist.cpp:110: error: ‘OCCUPATION’ is not a type
../src/multilist.cpp:111: error: ‘LOCATION’ is not a type

回答

5

你需要把這些構造函數調用中的Multilist構造:Multilist(...) : catId(ID), catOccupation(OCCUPATION), ...,並從聲明中刪除。您目前的使用情況看起來像您正在嘗試聲明函數返回mlCat<>因此ID et。 al被解釋爲參數的類型。

+0

確實如此。謝謝 :) – 2011-04-11 04:48:23

0

不能分配構件(特別是非靜態的)變量class體內:

class A { 
    int i = 0; // error: member assignment not allowed in current C++ standard 
    int j(2); // error: compiler thinks 'j' is a function; with argument type as 2 
    int k;  // ok 
}; 

被調用的Multilist對象時,像mlCat<>構件可在其構造初始化。