obvious error to this class/subclass construction?

B

Bint

Hello,

Is there something wrong with this class/subclass declaration that would
cause the error "Error : cannot construct prefboxtoggle's base class
'boxvaltoggle'"?
I don't see why it shouldn't work.
Thanks for any help!
B

class boxvaltoggle { public:
u8 *val;
int atx, aty, w, h;
void (*hitfunction)(void *);

boxvaltoggle(int ax, int ay, int ww, int hh, u8 *v) {
atx = ax;
aty = ay;
w = ww;
h = hh;
val = v;
}
};


class prefboxtoggle:public boxvaltoggle { public:
const char *title, *selection0, *selection1;

prefboxtoggle(int ax, int ay, int ww, int hh, const char * t, u8 *v,
const char *s1, const char *s2) {
boxvaltoggle(ax,ay,ww,hh,v);
title = t;
selection0 = s1;
selection1 = s2;
}
void Draw(Boolean lit);
u8 Do(u8 x, u8 y, u8 button);
};
 
V

Victor Bazarov

Bint said:
Is there something wrong with this class/subclass declaration that
would cause the error "Error : cannot construct prefboxtoggle's
base class 'boxvaltoggle'"?
I don't see why it shouldn't work.
Thanks for any help!
B

class boxvaltoggle { public:
u8 *val;
int atx, aty, w, h;
void (*hitfunction)(void *);

boxvaltoggle(int ax, int ay, int ww, int hh, u8 *v) {
atx = ax;
aty = ay;
w = ww;
h = hh;
val = v;
}
};


class prefboxtoggle:public boxvaltoggle { public:
const char *title, *selection0, *selection1;

prefboxtoggle(int ax, int ay, int ww, int hh, const char * t, u8
*v, const char *s1, const char *s2) {
boxvaltoggle(ax,ay,ww,hh,v);

Are you trying to construct the base class? That's done in the
constructor _initialiser_list_. Please read up on those.
title = t;
selection0 = s1;
selection1 = s2;
}
void Draw(Boolean lit);
u8 Do(u8 x, u8 y, u8 button);
};

V
 
B

Bint

Thanks,

I figured out how to call the base class constructor via the list -- but i
need to call it *after* I get the width and height arguments from within the
sublcass constructor. Can I do that?

Bob
 
S

Salt_Peter


Try not to Top-Post so we can all follow the discussion in sequence.
[rearranged inline...]
I figured out how to call the base class constructor via the list -- but i
need to call it *after* I get the width and height arguments from within the
sublcass constructor. Can I do that?

Bob

No, not *after* you get the parameters, otherwise a default ctor for
the base class needs to be supplied.
You didn't provide a default ctor so you get the error.
Before you enter a constructor's block {...} an instance's base and
members must be allocated.
This implies that appropriate ctors must be available and accessible.
Thats why init lists are provided so you can initialize these at
construction time.

It would so much simpler if you showed an example that simplifies your
issue.

class Base
{
int x;
int y;
public:
// no default ctor supplied
// parametized ctor
Base(int x_, int y_) : x(x_), y(y_)
{
}
};

class Derived : public Base
{
double d;
public:
// Derived() : d(0) { } // error, no def ctor for Base available
// Derived(double d_) : d(d_) { } // error, no def ctor for Base
available
// parametized ctor with init list
Derived(int x_, int y_, double d_) : Base(x_, y_), d(d_)
{
}
};

int main()
{
Derived instance(10, 10, 99); // ok
}
 
N

Nick Keighley

I fixed your top post and removed sigs

I figured out how to call the base class constructor via the list -- but i
need to call it *after* I get the width and height arguments from within the
sublcass constructor. Can I do that?

won't this work?

prefboxtoggle(int ax, int ay, int ww, int hh, const char * t,
u8 *v, const char *s1, const char *s2):
boxvaltoggle(ax,ay,ww,hh,v),title (t),selection0(s1),selection1
= s2
{
}

I'm a bit dubious that all your data is public...
Should a preboxtoggle really inherit from a boxtoggle?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top