Adding the value a pointer points to to an object array
Sep 1, 2013 at 8:42pm UTC
Hello everyone I am having a hard time with this. I have been at this since 5:00 this morning. I am working on the command pattern for design patterns class. My book is in Java so i am translating the book examples to c++. in this code I am trying to assign the object that two pointers point to to an object array. Gcc keeps pointing to the assignment operator in the remote control constructor .Here is the error I get:
1 2 3
/home/Copy/C++_Projects/Design_Patterns/Command_Pattern_CL/remote_control.h:19:25: error: no match for
‘operator =’ in ‘((RemoteControl*)this )->RemoteControl::m_on_commands[slot]
= on_command’
There is a total of 21 classes in this program. So I tried to only post what I think is nessary please let me know if you need more I will post on paste bin. I cant even start on my actual assigment untill I get this working. Thanks .
1 2 3 4 5 6 7
class Command
{
public :
Command() { }
virtual void execute() { }
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class RemoteControl
{
public :
RemoteControl() {
for (int index =0; index < NUMBER_OF_COMMANDS; index++) {
m_on_commands[index] = no_command;
m_off_commands[index] = no_command;
}
}
void set_command(int slot, Command *on_command, Command *off_command) {
m_on_commands[slot] = on_command;
m_off_commands[slot] = off_command;
}
void button_was_pressed(int slot) {
m_on_commands[slot].execute();
}
private :
Command no_command;
Command m_on_commands[NUMBER_OF_COMMANDS];
Command m_off_commands[NUMBER_OF_COMMANDS];
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
int main(int argc, char **argv)
{
RemoteControl remote_control;
GarageDoor g_door;
Light light_off;
Light light_on;
GarageDoorOpenCommand *g_door_cmd = new GarageDoorOpenCommand(g_door);
LightOnCommand *l_on = new LightOnCommand(light_on);
LightOffCommand *l_off = new LightOffCommand(light_off);
CeilingFan *ceilingFan = new CeilingFan("Living Room" );
CeilingFanOffCommand *fan_off_cmd = new CeilingFanOffCommand(ceilingFan);
CeilingFanOnCommand *fan_on_cmd = new CeilingFanOnCommand(ceilingFan);
Hottube *hottub = new Hottube( );
HotTubOnCommand *tub_on_cmd = new HotTubOnCommand(hottub);
HotTubOffCommand *tub_off_cmd = new HotTubOffCommand(hottub);
LivingRoomLightOffCommand *room_light_off_cmd = new LivingRoomLightOffCommand(light_off);
LivingRoomLightOnCommand *room_on_cmd = new LivingRoomLightOnCommand(light_on);
Stereo *stereo = new Stereo("Back bed Room" );
StereoOffCommand *stereo_off_cmd = new StereoOffCommand(stereo);
StereoOnWithCdCommand *stereo_on_cd = new StereoOnWithCdCommand(stereo);
remote_control.set_command(0, *stereo_on_cd, *stereo_off_cmd);
remote_control.set_command(1, room_on_cmd, room_light_off_cmd);
remote_control.set_command(2, tub_on_cmd, tub_off_cmd);
remote_control.set_command(3, fan_on_cmd, fan_off_cmd);
remote_control.button_was_pressed(0);
remote_control.button_was_pressed(1);
remote_control.button_was_pressed(2);
remote_control.button_was_pressed(3);
return 0;
}
EDIT: I went ahead and posted the whole program on pastebin for anyone who has the time to look at. Thanks alot.
//http://pastebin.com/j8hGCrMM
Last edited on Sep 2, 2013 at 12:14pm UTC
Sep 1, 2013 at 8:53pm UTC
You never overloaded the = operator for your command class, but use it in the constructor to the RemoteControl class.
Sep 1, 2013 at 8:58pm UTC
Thanks for the reply I thought of that but how do I do that for an abstract class the command class has nothing in it but a virtual function.
Topic archived. No new replies allowed.