Passing Class Instance throught pointer

I had create two instance of class, one who's a Character the other one is Wizard who inherit from Character. In my case you are playing as a Wizard and I was trying to automate the combat by turn.
I was wondering if it was better if make a specific class for the turn order between the Player and the A.I or am I better to go with 2 function in my Main.cpp 1 for AI turn and the other one for the Player Turn, I had already try to go with the second option; The functions, But I had no choice to make both class instance in the beginning of my main ().
I created 2 pointer that point to the respective instance of the object and I was trying to pass em by the function parameter , I'll show you my Main.cpp

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 //Main.cpp

 #include <iostream>
#include "Character.h"
#include "Wizard.h"

using namespace std;

void player_turn (Wizard*,Character*);


void ai_turn();



int main()
{

    Wizard Luis;
    Character Gerry ;
    Wizard* pLuis = 0; // initialize to zero
    Character* pGerry = 0;// initialize to zero
    pLuis = &Luis;  
    pGerry = &Gerry;

    cout<<"You are Luis the Wizard and you are facing Gerry you Opponent"<<endl<<endl<<"What you want to do to gerry?"<<endl<<endl;
    do

{
    player_turn(pLuis,pGerry);
    ai_turn();


    cout<<endl<<"Luis ";
    Luis.showlife();
    cout<<"Gerry ";
    Gerry.showlife();

}
while (Gerry.is_alive() == true && Luis.is_alive() == true ) ;

if (Gerry.is_alive() == false)
    cout<<"Gerry is dead"<<endl<<endl;

else if (Luis.is_alive() == false)
    cout<<"Luis is dead"<<endl<<endl;



    return 0;
}

void player_turn (Luis,Gerry)
 {
    char player_c;
    cout<<"Normal Attack (N)"<<endl<<"Throw a Fireball (F)"<<endl<<"Drink a Potion (P)"<<endl<<endl;
    cin>>player_c;

    if (player_c == 'n' )
  {
    cout<<endl<<"Luis";
    Luis.norm_attack(Gerry);
    cout<<"Gerry"<<endl<<endl;
  }

    else if (player_c == 'f' )
  {
    cout<<endl<<"Luis";
    Luis.fireball_cast(Gerry);
    cout<<"Gerry"<<endl<<endl;
  }

     else if (player_c == 'p' )
  {
    cout<<endl<<"Luis";
    Luis.heal(20);
  }

    else
  {
    cout<<endl<<"Enter a Valid Choice "<<endl;

  }
  }

  void ai_turn()
   {
    cout<<endl<<"Enter a Valid Choice "<<endl;

  }


The compiler is telling me that Luis and Gerry wasn't declare in the player_turn ()

and error: variable or fiel player_turn declared void?

What did I misunderstood?

Thanks for helping!!
Last edited on
The signature of the function on line 53 needs to be the same as the prototype:

Line53: void player_turn (Wizard *Luis, Character *Gerry)

Change the following:
1
2
3
4
5
6
7
8
9
10
11
12
    Wizard Luis;
    Character Gerry ;
    Wizard* pLuis = 0; // initialize to zero
    Character* pGerry = 0;// initialize to zero
    pLuis = &Luis; 
    pGerry = &Gerry;

    cout<<"You are Luis the Wizard and you are facing Gerry you Opponent"<<endl<<endl<<"What you want to do to gerry?"<<endl<<endl;
    do

{
    player_turn(p&Luis,p&Gerry);


I better to go with 2 function in my Main.cpp
Right now I would think you're better off with two functions. Otherwise you need to make the classes smarter...
Last edited on
Thank you for your help , I now understand more the whole concept of pointer
Topic archived. No new replies allowed.