开发者

Simple C++ template definition problem

开发者 https://www.devze.com 2023-01-23 02:30 出处:网络
I\'m new to C++. Here is the code: template <class T> typename lw_slist { // .... some code private:

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开发者_JS百科

Why?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消