开发者

C++ Stack Declaration Issue

I'm trying to declare a stack in my program, however it says I can't declare it the way I'm trying to.

private:
    stack<tree_node<T>*> s;

Then I try to use it as follows:

 protected:
     s.push(p);

Note: there is more code in the "protected" section, but th开发者_JS百科is is where the error is coming from.

error: ISO C++ forbids declaration of ‘stack’ with no type


At least in a typical case, the <T> only make sense inside a template where T is declared as one of the template parameters, something like:

template<class T>
class whatever // ...


The issue here, from what you've posted, is that you are attempting to put naked code in the class declaration. You put implementation details in a function.

This is wrong:

class foo{
    int bar;
protected:
    bar = 3;
};

This is correct:

class foo{
    int bar;
protected:
    void setBar(){ bar = 3; }
]:


The problem has to do with not having std::stack. Thanks!


When you say

protected: s.push(p);

do you mean you are trying to write this inside the class declaration (the .h file)? You have already declared the stack, to use you need to do so inside the definition of a member function.

Also, as mentioned by Jerry Coffin, unless you have typedef-ed "T" to a user type, you should replace it with the actual type you will use. Or make the whole class a template class...

You say you have more code in the "protected" area, perhaps you should post that as well and also explain what you are trying to accomplish.


Try

stack<tree_node<T>>* s;

or are you tryin to create a stack of tree node pointers?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜