0

I am doing a project where I recreate Pokémon in C++ and currently I am in the process of implementing moves.

My implementation is currently:

#pragma once

class Pokemon;
class MoveBase

{
public:
    void use(Pokemon* target)
    {
        target->setHealth(damage);
    }

private:
    int damage;

};

where my thought-process is that I need a target pointer (pointer that points to the pokémon which the move will be used on) so that I can properly decrease the HP of the target pokémon accordingly. However, I do not want the circular dependency if I include the Pokemon-class so I forward declare Pokemon which I thought should've worked, but I just get

use of undefined type 'Pokemon' at line 10

I am relatively new in C++ and would like to know what is wrong with this code snippet. Did I forward declare incorrectly?

  • 1
    A forward declaration only tells the compiler that the class exists, but it doesn't tell the compiler anything about its members. For that you need the full class definition. You can use the forward declaration of `Pokemon` to *declare* the `use` function. But to define (implement) the `use` function you need the full `Pokemon` class definition, with all its public members. – Some programmer dude Nov 22 '22 at 08:29
  • You can use forward declaration only to declare types. When you actually **use** this type you need full declaration. So, either include header file with full description of `Pokemon` class or move function's `void use(Pokemon* target)` implementation to .cpp file. – sklott Nov 22 '22 at 08:30
  • The solution for your case might be to use forward declarations in *one* header file, and include it in the other. *Or* move function definitions (implementations) into source files which can include both header files in any order. – Some programmer dude Nov 22 '22 at 08:30
  • 1
    Does this answer your question? [Member acces into incomplete type C++](https://stackoverflow.com/questions/70020849/member-acces-into-incomplete-type-c) – alagner Nov 22 '22 at 08:30
  • Also, [What are forward declarations in C++?](https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c) – Jason Nov 22 '22 at 08:32

0 Answers0