physics.h
#ifndef PHYSIO_PHYSICS
#define PHYSIO_PHYSICS
#pragma once
#include "consts.h"
#include "particle.h"
class Particle; // <- Forward declaration!!!
class Physics {
public:
Particle *particle;
int mass;
bool _static;
Physics() = default;
Physics(Particle *particle, int mass, bool _static) {
this->particle = particle;
this->mass = mass;
this->_static = _static;
}
void Update(Particle *, int, bool);
bool Collides(Rectangle);
bool CollidesGround();
void HandlePhysics(float);
};
#endif
particle.h
#ifndef PHYSIO_PARTICLE
#define PHYSIO_PARTICLE
#pragma once
#include "../raylib/include/raylib.h"
#include "consts.h"
#include "physics.h"
class Physics; // <- Also forward declaration!!!
class Particle {
public:
Vector2 position;
Color color;
int mass = DEFAULT_MASS;
int size = MINIMAL_PARTICLE_SIZE;
bool drawable = true;
bool _static = DEFAULT_STATIC;
Physics physics;
Particle(Vector2 position, Color color) {
this->position = position;
this->color = color;
this->physics.Update(this, this->mass, this->_static);
}
Particle(Vector2 position, Color color, int size) {
this->position = position;
this->color = color;
this->size = size;
this->physics.Update(this, this->mass, this->_static);
};
Particle(Vector2 position, Color color, int size, bool _static) {
this->position = position;
this->color = color;
this->size = size;
this->_static = _static;
this->physics.Update(this, this->mass, this->_static);
}
void SetPosition(Vector2);
void SetColor(Color);
void SetMass(int);
void SetStatic(bool);
void SetSize(int);
void Draw();
};
#endif
Running the code with:
g++ physio.cpp -o physio.exe -O1 -Wall -Wno-missing-braces -I raylib/include -L raylib/lib -lraylib -lopengl32 -lgdi32 -lwinmm -std=c++17 physio/particle.cpp physio/particlesbuff.cpp physio/physics.cpp && physio.exe
Error:
In file included from physio/physics.h:8,
from physio/physics.cpp:1:
physio/particle.h:28:13: error: field 'physics' has incomplete type 'Physics'
28 | Physics physics;
| ^~~~~~~
physio/particle.h:12:7: note: forward declaration of 'class Physics'
12 | class Physics;
| ^~~~~~~
I actually tried to google, but I have only two choices: forward declaration (i’m using it, but it doesn’t works), or completely move Physics to Particle (bad choice I think).
What’s the problem and how to fix it?
>Solution :
You have a circular dependency issue. You have physics.h including particle.h, which in turn includes physics.h.
The definition of Particle needs the full definition of Physics, but Physics only needs the declaration of Particle.
In physics.h, remove #include "particle.h". The forward declaration is all that’s needed there. And in particle.h, remove the forward declaration, as the inclusion of physics.h will give you the definition.