expected constructor, destructor, or type conversion before ‘*’ token

Could you help me with this error?

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Stack
{
        private:
                struct Element
                {
                        struct Element *next;
                        void *data;
                };

                Element *head;
        public:
                Stack();
                ~Stack();
                void push(void *data);
                void *pop();
                Element * findMToLastElement(int m);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Element * Stack::findMToLastElement(int m)
{
        Element *current = head;
        Element *mbehind;
        while(m)
        {
                if(current->next)
                        current = current->next;
                else
                        return NULL;
                m--;
        }
        mbehind = head;
        while(current->next)
        {
                current = current->next;
                mbehind = mbehind->next;
        }
        return mbehind;
}
Element is inside Stack, so you should use Stack::Element when outside Stack
Thanks for that tip. Can you tell why I get following error


stack.c++:11: error: ISO C++ forbids declaration of ‘Element’ with no type
stack.c++:11: error: expected ‘;’ before ‘*’ token
stack.c++:62: error: no ‘Stack::Element* Stack::findMToLastElement(int)’ member function declared in class ‘Stack


when I use Stack as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Stack
{
        public:
                Stack();
                ~Stack();
                void push(void *data);
                void *pop();
                Element * findMToLastElement(int m);
        private:
                struct Element
                {
                        struct Element *next;
                        void *data;
                };

                Element *head;
};


Is it a must to declare the data members before using it, as I did on the first post?
You can try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Stack
{
        struct Element; // forward declaration
    
        public:
                Stack();
                ~Stack();
                void push(void *data);
                void *pop();
                Element * findMToLastElement(int m);
        private:
                struct Element
                {
                        struct Element *next;
                        void *data;
                };

                Element *head;
};
thanks..
Topic archived. No new replies allowed.