-2

There is a problem in the simple code snippet below that i can't uncover even with a debugger. Thanks for your help in advance.

Command.h

class word{

   private : static int next_word;
   private : int word_id;
   public : word();
   public : void grabIt();
   public : void raiseIt();
   public : void putItDown();
};
class Command{

   public : typedef void(word::*Method)();
   private : word* w;
   private : Method m;
   public : Command(word *w, Method m);
   public : void execute();
};
template<typename T>
class Queue{

  private : enum{
    SIZE=9
  };
  private : T* commandArray[SIZE];
  private : int m_added = 0, m_removed = 0;

  public : void enqueue(T* t){

        commandArray[m_added] = t;
        m_added = (m_added + 1) % SIZE;
    }
  public : T* dequeue(){
        int temp = m_removed;
        m_removed = (m_removed + 1) % SIZE;
        return commandArray[temp];
    }
};

Command.cpp

int word::next_word = 0;
word::word(){

   this->word_id = next_word++;
}
void word::grabIt(){
  std::cout << "Grabbed it" << std::endl;
}
void word::raiseIt(){
  std::cout << "Raised it" << std::endl;
}
void word::putItDown(){
   std::cout << "Put it down" << std::endl;
}
Command::Command(word *w, Method m){
   w = w;
   m = m;
}
void Command::execute(){
   (w->*m)(); // -------------->>>>> Causing Seg-Fault
}

Main.cpp

Queue<Command> queue;
Command *commandArray[9];
Command command1(new word, &word::grabIt);       commandArray[0] = &command1;
Command command2(new word, &word::raiseIt);      commandArray[1] = &command2;
Command command3(new word, &word::putItDown);    commandArray[2] = &command3;
Command command4(new word, &word::grabIt);       commandArray[3] = &command4;
Command command5(new word, &word::raiseIt);      commandArray[4] = &command5;
Command command6(new word, &word::putItDown);    commandArray[5] = &command6;
Command command7(new word, &word::grabIt);       commandArray[6] = &command7;
Command command8(new word, &word::raiseIt);      commandArray[7] = &command8;
Command command9(new word, &word::putItDown);    commandArray[8] = &command9;

for( int i=0 ; i < 9; i++){
   queue.enqueue(commandArray[i]);
}
for( int i=2 ; i < 6 ; i++){
   queue.dequeue()->execute();
}

Some word objects inside the queue appear NULL in the debugger, so cause Seg-Fault.

RPichioli
  • 3,245
  • 2
  • 25
  • 29
  • 2
    I'm pretty sure that in the `Command` constructor you intended to write `this->w = w;`, etc... Think about what you actually wrote. – paddy Oct 05 '16 at 11:02
  • @paddy: this is not java, it is supposed to work in c++: http://stackoverflow.com/questions/268587/can-i-use-identical-names-for-fields-and-constructor-parameters. As long as the initializer list is used. (Which is apparently not.) – Trevir Oct 05 '16 at 11:11
  • I am not saying that is good C++ style, but it's easier to get them to think about the difference using that example than it is to suggest initializer lists which they clearly don't understand. – paddy Oct 05 '16 at 11:18

1 Answers1

0

To fix the problem, change

Command::Command(word *w, Method m){
   w = w;
   m = m;
}

to

Command::Command(word *w, Method m) : w(w), m(m){}

For the exact reasoning, look at Can I use identical names for fields and constructor parameters?

Community
  • 1
  • 1
Trevir
  • 1,253
  • 9
  • 16
  • Thank you very much, it is working now. I didn't expect the function and constructor parameters to cause trouble like this because I am used to deploying them as above in java. – RyuichiShimura Oct 05 '16 at 13:17