classes - problems with a constructor

Well this is the very easy code I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class game
{
public: 
    game(int ww,int hh, int ss=30)
    {
        playfield(ww,hh);
        w = ww;
        h = hh;
        step = ss;
    }
private:
    field playfield;
    int w,h,step;
};

Where field is aclass with a constructor that takes two integers as arguments..

Now I thought such a thing would work, but apparantly it doesn't (else I wouldn't post here...):
error C2512: 'field' : no appropriate default constructor available
error C2064: term does not evaluate to a function taking 2 arguments
Are the errors I see. So how would i go about this? - I want the objects "game" to have a private member "playfield" (which itself is an object from the class "field". Yet I want it to be "set" through the constructor (since I need to pass the width and height to it), I can't really fill in some "default" values as that wouldn't make sense at all.
field playfield;

Class field needs a default constructor.

term does not evaluate to a function taking 2 arguments

What does the term function look like?
Uhm, well I "hoped" that part called the (non default) constructor.. Like I said: the constructor should initialize an array: and the width/height of that array is given from the main function (the one that also creates the "game" object).


So what I want is that "game" HAS_A "field", and that it "passes" the arguments width and height..

I know why, I get the problem: and what's causing it.. However I don't know how I could solve it...
Last edited on
1
2
3
4
5
6
game(int ww,int hh, int ss=30) : playfield(ww,hh)
    {        
        w = ww;
        h = hh;
        step = ss;
    }


Edit: Personally, I'd use a pointer to field and then just use new in the constructor.
Last edited on
You can't manually construct static members. If you need to pass parameters to the constructor, use pointers:
1
2
3
4
5
6
struct B{
    A *a;
    B(){
        a=new A(1,2);
    }
};

But what the compiler is complaining about is that, by declaring field::field(int,int), you, so to speak, undeclared the default constructor (field::field()) which is what the game::game(int,int,int) calls before the start of your code.

Edit: Oh... Slowpoke, me.
Last edited on
Topic archived. No new replies allowed.