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.

SorryEh
  • 900
  • 2
  • 15
  • 47
  • What's wrong with you have? That is, `cmds[get_command_count()].init(line);` – Robᵩ Apr 10 '12 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 – keety Apr 10 '12 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. – celtschk Apr 10 '12 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. – SorryEh Apr 10 '12 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. – SorryEh Apr 10 '12 at 23:44
  • "getting a segmentation fault" That would have good for us to know. Please produce the smallest **complete** program you can (it should be only 15-25 lines long) that demonstrates the problem you are having, and post that in your question. See http://sscce.org for more information. – Robᵩ Apr 10 '12 at 23:44
  • @Robᵩ the details are up. I know it's that line because things before it run up until that line. – SorryEh Apr 11 '12 at 00:01
  • @Umeed - Thank you for providing more information. Unfortunately, you have not provided a **complete** program. Please create the smallest complete program you possibly can that demonstrates the problem, and post that. – Robᵩ Apr 11 '12 at 00:34
  • @Umeed for a start you have not intialized `CmdCount`. – keety Apr 11 '12 at 01:01

1 Answers1

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.

SundayMonday
  • 19,147
  • 29
  • 100
  • 154