Toomtarm Kung
Toomtarm Kung

Reputation: 604

Could I define class inside struct in c++

I have problem about struct and class. Now I define class inside the struct and then I create the struct as node and push node in queue. The problem is when I pop queue I create the node to receive pop node but the class in node is a new one, not the same as before push. Follow as code.

    struct queueNode {
        Puzzle puzzle;
        queueNode *next;
        short lastMove;
    };

    class Puzzle {

    private :
        short field[4][4];
        short posBlankI;
        short posBlankJ;
    public :
        Puzzle();
        bool isFinish();
        void print();
        void create();
    }

    class Queue {
        private:
            queueNode *first, *last;

        public:
            Queue(){
                first = new queueNode;
                first->next = NULL;
                last = first;
            }
            ~Queue() { delete first; }
            bool isEmpty(){ return (first->next == NULL); }
            void push(queueNode *aux){

                last->next = aux;
                last = aux;

            }
            queueNode pop(){
                queueNode *aux = first;
                first = first->next;
                return *aux;
            }
    };

    //global variable      
    Queue *q = new Queue();

    int main(){
        queueNode *root = new queueNode;
        root->puzzle.create();
        q->push(root);
        q->pop().puzzle.print();
        return 0;
    }

Upvotes: 1

Views: 2773

Answers (5)

Dean Knight
Dean Knight

Reputation: 680

When you create the Queue, it creates a queueNode using new and puts it into the Queue by default. So when you call pop() it returns that default node you did not configure using create().

I modified your code a bit and got it to compile and run in order to illustrate the point I am making above.(Note: it compiles on VS2010, remove #include "stdafx.h" if you use something else)

Hopefully this helps you see exactly what you are doing.

I would not recommend using the code below in an implementation you care about. Use it as a learning tool only. I suggest separating out your class declarations into separate header files and the implementations into separate CPP files. It helps organize things.

output:

0000
0000
0000
0000
1111
1111
1111
1111

Code:

#include "stdafx.h"
#include <iostream>

class Puzzle {

    private :
        short field[4][4];
        short posBlankI;
        short posBlankJ;
    public :
        Puzzle()
        {
            for(int i = 0; i<4; i++)
            {
                for(int j = 0; j < 4; j++)
                {
                    field[i][j] = 0;
                }
            }
        }
        bool isFinish();
        void print()
        {
            for(int i = 0; i<4; i++)
            {
                for(int j = 0; j < 4; j++)
                {
                    std::cout<<field[i][j];
                }
                std::cout<<std::endl;
            }
        }
        void create()
        {
            for(int i = 0; i<4; i++)
            {
                for(int j = 0; j < 4; j++)
                {
                    field[i][j] = 1;
                }
            }
        }
    };
struct queueNode {
        Puzzle puzzle;
        queueNode *next;
        short lastMove;
    };



    class Queue {
        private:
            queueNode *first, *last;

        public:
            Queue(){
                first = new queueNode;
                first->next = NULL;
                last = first;
            }
            ~Queue() { delete first; }
            bool isEmpty(){ return (first->next == NULL); }
            void push(queueNode *aux){

                last->next = aux;
                last = aux;

            }
            queueNode pop(){
                queueNode *aux = first;
                first = first->next;
                return *aux;
            }
    };

    //global variable      
    Queue *q = new Queue();

    int main(){
        queueNode *root = new queueNode;
        root->puzzle.create();
        q->push(root);
        q->pop().puzzle.print();
        q->pop().puzzle.print();
        system("Pause");
        return 0;
    }

Upvotes: 1

Andriy
Andriy

Reputation: 8604

The problem has nothing to do with a class being member of a struct.

The constructor of Queue creates a queue with 1 blank node. q->push(root) inserts the 2nd node at the end. q->pop() returns the copy of the 1st (blank) node, not the 2nd as you expect.

Upvotes: 2

kai
kai

Reputation: 21

pop return a object not a pointer. A new queueNode is created and its content is copied from *aux. You should modify pop to return a pointer of queueNode.

Upvotes: 1

Pete Becker
Pete Becker

Reputation: 76458

Yes, you can define a class inside a struct. The code sample doesn't do that.

Upvotes: 5

Tom Tanner
Tom Tanner

Reputation: 9354

Your push adds your node to the end of the internal list. Which isn't empty.

The standard library has queues, lists, and all sorts of nice containers. Use one of those.

Upvotes: 0

Related Questions