0

My program wont seem to compile when I want to include Player.h inside Wall.h, however I can include Wall.h in Player.h. I am confused. See code below.

Player.h:

#pragma once
#include <SDL.h>
#include "Entity.h"
#include "Vec2.h"
#include "Wall.h"
#include "Timer.h"

namespace LyingD
{
class Player : public Entity
{
public:
    Player() = default;
    Player(float x, float y, float w, float h, float speed, float gForceX, float gForceY);
 
    Timer jumpTimer;
    bool jumping = false;
    
    Timer fallTimer;
    bool isFalling = false;

    bool onTheGround = false;

    Vec2 speed;
    Vec2 velocity;
    Vec2 acceleration;
    Vec2 gForce;
    
    void wallCollision(Wall walls[], int numOfWalls);

    void jump(Timer mainTimer, Wall walls[], int numOfWalls);
    void moveUp(Timer mainTimer, Wall walls[], int numOfWalls);
    void moveDown(Timer mainTimer, Wall walls[], int numOfWalls);
    void moveRight(Timer mainTimer, Wall walls[], int numOfWalls);
    void moveLeft(Timer mainTimer, Wall walls[], int numOfWalls);

    void gravity(Timer mainTimer, Wall walls[], int numOfWalls);
    
};

}

Wall.h:

#pragma once
#include <iostream>
#include <SDL.h>
#include "Entity.h"

namespace LyingD
{
class Wall : public Entity
{
public:
            
    Wall() = default;
    Wall(float x, float y, float w, float h) 
    {           
        this->size = Vec2(w, h);
        this->position = Vec2(x, y);
    }
};
}

Entity:

#pragma once
#include "SDL.h"
#include "Vec2.h"

namespace LyingD
{
class Entity
{
public:
    Entity() = default;
    Entity(float x, float y, float width, float height) :
        position(x, y), size(width, height) {}

    Vec2 position;
    Vec2 posBuffer = position;

    Vec2 size;
                    
    int checkCollision(Entity object);
};
}

The errors I get are:

1>C:\Users\Gebruiker\Desktop\C++ Projects\Developed\Camera.h(28,1): error C2660: 'LyingD::Player::wallCollision': function does not take 2 arguments
1>C:\Users\Gebruiker\Desktop\C++ Projects\Developed\Player.h(29,8): message : see declaration of 'LyingD::Player::wallCollision'

It does that for every instance of Wall.

However, it does take 2 arguments.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • What compiler do you use? – JohnFilleau Dec 18 '20 at 18:34
  • 1
    See this question for information on how to dump the preprocessor output of your program. That shows the "real" file that the compiler attempts to compile. And this should explain why the compiler complains. Probably. You haven't told us what the error actually is. https://stackoverflow.com/questions/3742822/preprocessor-output – JohnFilleau Dec 18 '20 at 18:36
  • I use visual studios 19. And thanks, I will definitely look that up! – Lyingdutchman Dec 18 '20 at 18:38
  • 1
    "_But can use the other header in the header. Confused_" does not say much about the actual error message you get. Copy/paste the _exact_ message into the question. – Ted Lyngmo Dec 18 '20 at 18:39
  • 2
    Sounds a lot like a circular dependency, but not enough information ([mre] strongly recommended) to give any concrete advice or answer. – user4581301 Dec 18 '20 at 18:40
  • 2
    What does `Entity.h` look like? Both `Player.h` and `Wall.h` are including it, does it in turn include them? That would create circular references. And why do you want to include `Player.h` in `Wall.h` if the `Wall` class does not refer to the `Player` class? – Remy Lebeau Dec 18 '20 at 18:42
  • I havent put any code in their because well, it doesnt work. So thats why there is no reference yet. I posted the Entity file as well. And no, i dont include them in their. – Lyingdutchman Dec 18 '20 at 18:46
  • Well, now you need to show `Camera.h` (where is that being used?), since that is where the *real* error is occurring. Is there maybe an (older?) version of `Player.h` on your system where `wallCollision()` doesn't take any parameters? Or, does `Camera.h` use any forward declarations for `Player`? – Remy Lebeau Dec 18 '20 at 18:47
  • Even if I delete every file of Camera.h, it just gives the error for another instance of every object of every argument of wall.. And no, Player.h is a completely new file. I am pretty sure, as this is my only project on my computer. – Lyingdutchman Dec 18 '20 at 18:48
  • 1
    The error messages you have shown are not about `Wall`, they are about `Player`, so why are you focusing on `Wall`? Are you getting *other* errors you haven't shown yet? Without a [mcve], we can't help you. You are leaving out important details. – Remy Lebeau Dec 18 '20 at 18:49
  • Every header has a #pragma once, so it shouldnt be the problem. And yeah, im sawry. I cant give much more information as thats as much as i understand about it. Im fairly new here and just trying to learn all this stuff on my own. And yes, im sorry,. I ment player. Its about player :P – Lyingdutchman Dec 18 '20 at 18:52
  • ***Every header has a #pragma once, so it shouldnt be the problem.*** That is not the solution to a circular loop of headers. Removing the loop and using forward references is. However with that said I am not sure where your errors come from. – drescherjm Dec 18 '20 at 18:53
  • Hmm... Ill look into that @drescherjm ! – Lyingdutchman Dec 18 '20 at 18:55
  • I think with all the bits of info I got from you guys it should be enough for me to find the problem and solution! Thanks :D – Lyingdutchman Dec 18 '20 at 18:56
  • Related to resolving circular header dependencies: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Dec 18 '20 at 18:56
  • appreciated @drescherjm ! – Lyingdutchman Dec 18 '20 at 18:58

0 Answers0