Chan Le
Chan Le

Reputation: 2204

G++ compiler doesn't recognize the enum type in C++

I tried to use enumerated type and template class, but i have no idea why the following code doesn't work:

#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 return an error in the 'CATEGORY DEFINITION' part:

../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

Upvotes: 1

Views: 984

Answers (2)

iammilind
iammilind

Reputation: 69988

You can not assign member (especially non-static) variables inside the class body:

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

when object of Multilist is invoked, members like mlCat<> can be initialized in its constructor.

Upvotes: 0

Ben Jackson
Ben Jackson

Reputation: 93710

You need to put those constructor calls in the constructor of Multilist: Multilist(...) : catId(ID), catOccupation(OCCUPATION), ... and remove them from the declarations. Your current usage looks like you are trying to declare functions returning mlCat<> thus ID et. al are being interpreted as the types of the arguments.

Upvotes: 5

Related Questions