0

i am working on a rpg games with class i created a struct call Character with ActionList* which store the instance. GeneralPlayer is a class where there have still a bunch of other players classes inherited it. This is my header file:

class Battle
{
public:
       struct Character
      {
        char type;//monster or player?
        bool alive;
        void*instance;//pointer to instance
      };
      Battle(GeneralPlayer*,AbstractMonster*,int,int,int);     
      Battle(GeneralPlayer*, AbstractMonster*, int, int);
private:
       Character *ActionList;
};

i was trying to convert GeneralPlayer* to void*. However seems like the code doesnt work as i thought. P and M are array of pointers of those player classes.

 Battle::Battle(GeneralPlayer*P, AbstractMonster*M, int a, int b, int c)
{
   a = numP;
   b = numM;
   c = turn_limit;
   ActionList = new Character[numP + numM];
   P = new GeneralPlayer[numP];
   for (int i = 0; i < numP; i++)
   { 
      ActionList[i] = static_cast<void*>(P[i]);
      ActionList[i].type = 'p';
   }
  for (int i = numP; i < numP+numM; i++)
  {
      ActionList[i] = static_cast<void*>(M[i]);
      ActionList[i].type = 'm';
  }
}

it keeps showing the error C2440. I wish can solve my problem with anyone helps thank you.

7
  • 3
    P[i] is not a GeneralPlayer*, it's a GeneralPlayer. May 17, 2016 at 14:04
  • Do you mean that after you called the function, the variable you passed as P doesn't seem to be initialized? Please try to elaborate, and also please try to create a Minimal, Complete, and Verifiable Example to show us. May 17, 2016 at 14:06
  • @songyuanyao sorry i am still beginner. if P[i] isnt GeneralPlayer*, then what should i suppose to use?
    – Shi Yi Lee
    May 17, 2016 at 14:07
  • If my above comment isn't the problem (yet), and if you have e.g. build errors, then please edit the question to include the complete and unedited build-log. If you haven't done so yet, please read about how to ask good questions. May 17, 2016 at 14:08
  • 2
    You never need a static_cast to conver a pointer to anything to void*. This conversion happens automatically.
    – SergeyA
    May 17, 2016 at 14:10

2 Answers 2

5

You are trying to convert object into pointer, use the & operator to get the pointer in question.

ActionList[i] = (void*)&P[i];
ActionList[i] = (void*)&M[i];
3
  • You don't need the explicit (and even c-style) cast. Any data pointer type can be implicitly cast to void* May 17, 2016 at 14:10
  • Also, Character *ActionList should maybe be of data type Character **, Character ** ActionList = new Character*[numP + numM]; May 17, 2016 at 14:14
  • This is true, but doesn't answer the question (ActionList[i ].instance = (void*)&P[i]; would) or solve the OP's problem. Plus why would you want to discard all of the type-checking goodness the compiler can do for you? May 17, 2016 at 15:23
4

One of the problems here is that the Character structure is not a parent of either GenericPlayer or AbstractMonster. It seems that the Character::instance member should be pointing to the player or monster, which means your code should be something like

ActionList[i].type = 'p';
ActionList[i].alive = true;
ActionList[i].instance = &P[i];

This is assuming that the list of players is already initialized by the caller of the Battle constructor, then you should not allocate a new array of players, so the P = new GenericPlayer[numP]; statement should be removed.

It should be noted that having something like you do, a "generic pointer" (what void * is) and then a member saying what type it's really pointing to is considered bad design. Instead you would have a common base-class for both monsters and players, and use a pointer to that. Then with the correct use of polymorphism and virtual member functions you don't need the type field. And then it's easy to refactor the code to use some other means of telling if a player or monster is alive or not, and then you don't need the Battle::Character class at all, and could use an array of pointers to the common base class instead, thus simplifying the code a bit, which is very good for maintainability.


There are a few other problems with the code as you show it, things that will cause problems later at runtime.

One problem is that in the loop iterating over the monsters, you initialize o to numP and loop up to numP + numM, but if the array M doesn't contain numP + numM elements you will go out of bounds.

Instead I suggest you do e.g.

for (int i = 0; i < numM; i++)
{ 
    ActionList[i + numP].type = 'm';
    ActionList[i + numP].alive = true;
    ActionList[i + numP].instance = &M[i];
}
4
  • @ShiYiLee Lets assume you have a function void f(int a) { a += 5; }. Now when you call the function, say f(some_int_variable), would you expect some_int_variable to change? It's the same thing here with your pointer variable P in the Battle constructor function, inside the function P is a local variable, all modifications to P will only be local inside the function. When you assign to P (P = new ...) then it's only inside the function, the variable you use in the call will not be modified. May 17, 2016 at 14:24
  • @ShiYiLee The problem discussed in my previous comment is solved by passing P by reference, the change is very simple: Battle(GenericPlayer*& P, ...). Adding the ampersand (&) there make P a reference to a pointer to GenericPlayer. May 17, 2016 at 14:26
  • but now i am having another problem "a value of type"GeneralPlayer*" cannot to be assigned to an entity of type "Battle::Character*" if i followed your steps
    – Shi Yi Lee
    May 17, 2016 at 14:32
  • @ShiYiLee Oh I see now that Character is a local type to the Battle class, and my assumption that GenericPlayer or AbstractMonster inherits from it is wrong. Instead you should set the instance member to point to the player/monsters. I'll update my answer. May 17, 2016 at 14:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.