Hey guys, I'm having trouble with making a constructor for my class. I've been using the
this keyword almost as you would with javascript, thinking it might work that way, and it hasn't. For context, I'm training myself in inheritance, and my project is to create video game objects (say Left4Dead will be an object), and have it inherit the members it should from the proper classes it belongs to (If Xbox360 is a class, and shooter is a subclass, it should inherit some of 360's members, and some of shooter's, just as a quick example). Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
enum func_ability{ high, medium, low};//this is a measure of the functionability of the motion controller for the console
class motion_control{
bool controller; //Do you use a controller like the wii, or your hands like kinect?
func_ability function;
float cost;
motion_control(bool ctrl, func_ability func, float price){
this.controller = ctrl;
this.function = func;
this.cost = price;
}
};
|
As you can see, it's like a JS constructor where I just say this.whatever = whatever, but apparently this isn't working. The error i get is
"error: request for member 'controller' in 'this', which is of non-class type 'motion_control* const'"
It repeats that error for every member in the constructor. Should I just pass the object being constructed as an argument instead, and construct it that way? The code would look like:
1 2 3 4 5
|
motion_control(bool ctrl, func_ability func, float price, motion_control obj){//the last arg, obj, is going to be the object itself//
obj.controller = ctrl;
obj.function = func;
obj.cost = price;
}
|
The creation of the object would look something like this:
1 2 3 4
|
/*Parameters explained in order: (uses a controller?, functionality?, price?, object being constructed?)*/
kinect(false, high, 150.00, kinect);
wii(true, low, 150.00, wii);
move(true, high, 100.00, move);
|
If I sound like a complete idiot, please forgive me; I am one lol.