Thread: Help needed with program.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Help needed with program.

    Im in the process of creating a bigger program which will allow two players to battle each other, but this will require me passing an array of pointers to objects of the FIGHTER class into a FightFunction function. Ive made a smaller example program to test this. The text I'm using to learn C++ doest actually say how to pass an array into a function so Ive tried passing the pointer to the array into the function, but the program Ive ended up with won't compile. As I'm new to this I'm assuming I've made an obvious mistake, or I'm not understnading it right so any help putting it right is much appreciated. It should be pretty obvious from the code what I want the program to do.

    Ive marked with numbers the lines that the compiler is finding errors on. The list of errors at the bottom.

    Thanks in advance.

    Code:
    #include <iostream>
    using namespace std;
    
    class FIGHTER
    {
      public:
         FIGHTER() { HP = 50; }
         ~FIGHTER();
         int SetHP(int hp) { HP = hp; }
         int GetHP() const { return HP; }
      private:
         int HP;
    };
    
    int FightFunction(int*);
    
    int main()
    {
      int error;
      FIGHTER *fighter_array[2];
      FIGHTER* ptr_a;
      FIGHTER* ptr_b;
      ptr_a = new FIGHTER;
      ptr_b = new FIGHTER;
      fighter_array[0] = ptr_a;
      fighter_array[1] = ptr_b;
      fighter_array[0]->SetHP(200);
      cout << "\n\nHP of player 1 before function: " << fighter_array[0]->GetHP();
      cout << "\nHP of player 2 before function: " << fighter_array[1]->GetHP();
    
      error = FightFunction(int *fight_array) //  Line 31 
    
      if (error == 0)
      {
        cout << "\n\n\n\nHP of player 1 after function: " << fighter_array[0]->GetHP();
        cout << "\nHP of player 2 after function: " << fighter_array[1]->GetHP();
      }
      else
      cout << "\n\n\n\nError";
    
      cin.ignore();
      cin.get();
      return 0;
    }
    
    int FightFunction(int *fighter_array)
    {
     fighter_array[1]->SetHP(300);  //  Line 48  
     return 0;
    }
    These are the compilier errors:

    "test.cpp": E2188 Expression syntax in function main() at line 31
    "test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function FightFunction(int *) at line 48

    It also warns that the fighter_array is never used in the function.

    Thanks again.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    you need to call the fighter_array to use the function.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    FightFunction should take FIGHTER *fighter_array[2], or FIGHTER *fighter_array[] or FIGHTER **fighter_array, because that's what you are trying to pass it (if you use either of the last two, you might want to also take the size of the array as an argument). Make sure to modify the prototype and the function definition.

    Then change the place where you call the function to just pass fighter_array directly.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > FIGHTER *fighter_array[2];
    Simplify - there's no need for arrays of pointers (yet)
    Say
    FIGHTER fighter_array[2];

    > int FightFunction(int*);
    Use the copy/paste rule - simply copy the declaration of the parameter you want to pass to the function into the declaration of the function.
    int FightFunction(FIGHTER fighter_array[2]);

    Then when you call the function, use the variable as you would any other variable being passed to a function
    FightFunction ( fighter_array );

    Inside the function, use the array exactly as you would use the array outside the function.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    I've think I've made all the changes Salem listed as so, but I'm still getting the following errors. I guess I'm still not understanding this properly. Any suggestions?

    "test.cpp": W8070 Function should return a value in function FIGHTER::SetHP(int) at line 9
    "test.cpp": E2034 Cannot convert 'FIGHTER *' to 'FIGHTER' in function main() at line 21
    "test.cpp": E2034 Cannot convert 'FIGHTER *' to 'FIGHTER' in function main() at line 22
    "test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function main() at line 23
    "test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function main() at line 24
    "test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function main() at line 25
    "test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function main() at line 31
    "test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function main() at line 32
    "test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function FightFunction(FIGHTER *) at line 44


    Code:
    #include <iostream>
    using namespace std;
    
    class FIGHTER
    {
      public:
         FIGHTER() { HP = 50; }
         ~FIGHTER();
         int SetHP(int hp) { HP = hp; }
         int GetHP() const { return HP; }
      private:
         int HP;
    };
    
    int FightFunction(FIGHTER fighter_array[2]);
    
    int main()
    {
      int error;
      FIGHTER fighter_array[2];
      fighter_array[0] = new FIGHTER;
      fighter_array[1] = new FIGHTER;
      fighter_array[0]->SetHP(200);
      cout << "\n\nHP of player 1 before function: " << fighter_array[0]->GetHP();
      cout << "\nHP of player 2 before function: " << fighter_array[1]->GetHP();
    
      error = FightFunction(fighter_array);
    
      if (error == 0)
      {
        cout << "\n\n\n\nHP of player 1 after function: " << fighter_array[0]->GetHP();
        cout << "\nHP of player 2 after function: " << fighter_array[1]->GetHP();
      }
      else
      cout << "\n\n\n\nError";
    
      cin.ignore();
      cin.get();
      return 0;
    }
    
    int FightFunction(FIGHTER fighter_array[2])
    {
     fighter_array[1]->SetHP(300);
     return 0;
    }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
      FIGHTER fighter_array[2];
      fighter_array[0] = new FIGHTER;
      fighter_array[1] = new FIGHTER;
    You don't need to do those last two lines that I've marked. When you declare your instance of the array of FIGHTER objects, the constructor gets called (twice) to build two FIGHTER objects for you.

    Code:
     fighter_array[0]->SetHP(200);
      cout << "\n\nHP of player 1 before function: " << fighter_array[0]->GetHP();
      cout << "\nHP of player 2 before function: " << fighter_array[1]->GetHP();
    
    ...
    
        cout << "\n\n\n\nHP of player 1 after function: " << fighter_array[0]->GetHP();
        cout << "\nHP of player 2 after function: " << fighter_array[1]->GetHP();
    
    ...
    
    int FightFunction(FIGHTER fighter_array[2])
    {
     fighter_array[1]->SetHP(300);
     return 0;
    }
    Try using the dot operator "." instead of "->".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    -> is for pointers.
    Code:
    aclass x, *p, &r = x;
    x.print();
    r.print();
    p->print();
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed with program - urgent thanks!
    By lildevil in forum C Programming
    Replies: 1
    Last Post: 03-09-2008, 06:45 AM
  2. Drawing program architecture - information needed
    By Marko_D in forum Windows Programming
    Replies: 1
    Last Post: 11-28-2003, 07:46 PM
  3. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM
  4. Program Ideas Needed
    By pkananen in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 10:08 PM