tags:

views:

70

answers:

2

I'm new to C++. Here is the code:

template <class T> typename lw_slist {
    // .... some code
    private:     

        typedef struct _slist_cell {
            _slist_cell *next;
            T data;
        } slist_cell;

        lw_slist::slist_cell *root;
};

Give this compilation error:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Why?

+8  A: 

It's an error because that isn't any kind of declaration at all.

The compiler needs to know if you want a class, struct, enum, union or something else. typename is not the right keyword to use there.

You are probably looking for this:

template<class T>
struct lw_slist {
};
Zan Lynx
Thank you for the answer. I've updated the question since that if I changed `typename` to `class`, the compiler then said `warning C4346: 'lw_slist<T>::slist_cell' : dependent name is not a type\nprefix with 'typename' to indicate a type`
solotim
+1  A: 

A whole new question, a whole new answer:

I think that you will want it like this:

template <class T>
class lw_slist {
    // .... some code
    private:     
        struct slist_cell {
            slist_cell *next;
            T data;
        };

        slist_cell *root;
};

There is no reason to use a typedef: C++ makes classes and structs automatically part of the namespace.

There is no reason to use lw_slist::slist_cell because slist_cell is already in the current namespace.

The reason that you were getting the error dependent name is not a type is that inside a template declaration C++ cannot tell if lw_slist<T>::slist_cell is supposed to be a type or a variable. It assumes a variable named slist_cell and you have to use typename to say otherwise.

Zan Lynx
Thank you very much. I'd better have a deep look at some tutorials.
solotim