0

I created a class in C++ called Commands (file name commands.cpp).

I've taken that and put it into a command array (file name test.cpp).

What I would like to know is how to call the functions that are within the Commands class.

for example I have a function within the Commands class called

     void command::init(char data[]) 
      {
        //detail
       }

and what i have tried to do to call the function is

EDIT

  Class test{
     int CmdCount;     // number of commands in the array
     int MaxCmds;      // max amount of commands allowed
     command* cmds;
  Public:
     int get_command_count() const{
          return CmdCount;
     }

     int readfile(const char fname[]){
          char line[161];

          FILE* fp;
          fp = fopen(fname, "r");

          if(fp){
               for(int i = 0; 1 == fscanf(fp, "%160[^\n]\n", line; i++){
                     cmds[get_command_count()].init(line);
                     CmdCount += 1;
               }
          }
          fclose(fp);
     }
  };

I just want to know how to be able to call void command::init(char data[]).

Any suggestions?

thanks.

9
  • What's wrong with you have? That is, cmds[get_command_count()].init(line); Commented Apr 10, 2012 at 23:30
  • is init a public method in class if so what you have should work as long as line is char* and cmds is populated correctly and get_command_count() returns a valid array index Commented Apr 10, 2012 at 23:33
  • get_command_count sounds as if it gives the total number of valid commands, in which case it's the first index which is not a valid command. Commented Apr 10, 2012 at 23:36
  • @Robᵩ that's the thing i believe it should be working unless I'm declaring my array wrong but it seems correct. I keep getting a segmentation fault. Commented Apr 10, 2012 at 23:36
  • @keety i tried char* but it wont work with my command class. It need's to be a char. Also, what did you mean by "populated correctly" ? thanks. Commented Apr 10, 2012 at 23:44

1 Answer 1

1

Sounds like your array contains instances of your class. In that case you want to call the method on a single entry in the array:

my_array[i].someMethod();

where my_array[i] is an instance of your class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.