Using class objects with stack stl

Hi all, this is my first post to the forums!

I'm currently working on designing my own card game while simultaneously trying to better my OO programming skills. Right now I have a class 'GettingStarted' that does all the work necessary for beginning the game. I also have another class 'MakeDeck' that contains all the data for the different cards in the game in an array of card objects. What I want to do is convert the array of card objects so that they are all held on a stack for gameplay (stack implemented using the stl <stack>).

My alternative to this would be creating my own class stack and then having my MakeDeck class inherit the stack functions. I would like to know however if there is a way to implement this using the stack stl.


I am getting a compiler error in visual studio
error C2027: use of undefined type 'MakeDeck'

Below I have put the code that I am trying to execute. I left out the public member functions because I don't think they aren't part of the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//GettingStarted.h

#include <iostream>
#include <stack>
#include <string>


class MakeDeck;

using namespace std;

class GettingStarted
{
   private:
     stack<MakeDeck> playerDeck;
   public:
     //member functions go here
};


I'm not sure if you need to see more code to diagnose my problem. If so, I'll put it up.

Thanks everyone.
That error's pretty clear. You only declared MakeDeck. You need to define it, at least with an empty definition ({}).
It depends on the implemention of std::stack, the std::stack is implemented by std::deque in VC and the deque needs the all information of its value_type, and hence it causes an error of imcomplete type.


1
2
3
4
5
6
//class MaskDeck;

class MaskDeck
{
    //...
}; //must give the definition before GettingStarted. 


or

1
2
3
4
5
6
7
8
class GettingStarted
{
   private:
     stack<MakeDeck*> playerDeck;
   public:
     //member functions go here
};


or
If you don't want to give the definition of MakeDec before GettingStarted, you can use std::list but you must the the definition of MakeDec in this compiling unit.
1
2
3
4
5
6
7
class GettingStarted
{
   private:
     std::list<MakeDeck> playerDeck;
   public:
     //member functions go here
};
class MakeDeck on line 8 in my example is there for class forwarding which I need to do because some of my member functions use objects from MakeDeck.

@jinhao:
stack<MakeDeck*> playerDeck worked and I no longer have the compiler error however I don't understand how exactly <MakeDeck*> differs from <MakeDeck>. Is <MakeDeck*> declaring a pointer to an object of the MakeDeck class that has not yet been instantiated? Does this have to do with .h files only dealing with static data?

Thanks again for all your help!
Because the std::deque has a static const member initialized by sizeof(MakeDeck), if you just give a declaraion of MakeDeck, the sizeof would not work because the MakeDeck is incomplete. Using MakeDeck* can fix this problem, because sizeof(a_non_function_pointer) is always equal to 4 in 32-bit compilier.
Oh okay, that makes sense. Thanks. :)

Is there a way I can mark this question as solved?
I like the pointer idea, but couldn't you just include MakeDeck.h?

MakeDeck.h:
1
2
3
4
class MakeDeck
{
    // ...
};


GettingStarted.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <stack>
#include "MakeDeck.h"

using namespace std;

class GettingStarted
{
private:
    stack<MakeDeck> playerDeck;
public:
    // ...
};
If I try to include MakeDeck.h, I get a compiler error saying there are multiple declarations of the MakeDeck class. I was told that I could get around that using the #ifndef preprocessor directive however that didn't seem to work.
where did u put the ifndef directive

it should be given in MakeDeck.h

it should enclose the defn like this

#ifndef _MakeDeck
#define _MakeDeck

class MakeDeck
{

//definition here
};

#endif
Topic archived. No new replies allowed.