2011-04-09 2 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; 
} 

이클립스에서 오류를 반환 '범주 정의'부분 : Multilist(...) : catId(ID), catOccupation(OCCUPATION), ...과 선언에서 제거 :

../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의 생성자에서 그 생성자 호출을 넣어해야합니다. mlCat<>과 같이 ID 등을 반환하는 함수를 선언하려는 것처럼 현재 사용법이 나타납니다. al은 인수의 유형으로 해석됩니다.

+0

사실입니다. 고맙습니다 :) –

0

당신은 class 체내 회원 (특히 비 정적) 변수를 할당 할 수 없습니다 : Multilist의 객체가 호출 될 때

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 
}; 

mlCat<> 같은 멤버는 생성자에서 초기화 할 수 있습니다.

관련 문제