Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Including header in a header - loop  (Read 1488 times)

0 Members and 1 Guest are viewing this topic.

jilda

  • Newbie
  • *
  • Posts: 5
  • dreaming in binary, living in real town
    • View Profile
Including header in a header - loop
« on: March 07, 2015, 01:10:42 pm »
I may be too stupid for this, but I can't really get my head around this:
I have files Header.h, Header.cpp, Quadtree.h and Quadtree.cpp.

Header.h
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "Quadtree.h" <------------------------------ CAUSES ERRORS, without it working fine

class Person
{
protected:
        ...
public:
        ...
        virtual void update(Quadtree quadtree); //<---- I NEED THE ABOVE INCLUDE FOR THIS ARG
};

Quadtree.h
#ifndef __QUADTREE_H__
#define __QUADTREE_H__

#include "Header.h"
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>

class Quadtree {
public:
        Quadtree(float x, float y, float width, float height, int level, int maxLevel);
private:
        ...

I need to pass a Quadtree instance to update function in Person. So what I tried was adding the include Quadtree.h, but that gives me loads of errors and I don't know how to fix it. I have the header guards in Header.h and Quadtree.h.. so what is the problem?

Here are the errors, I don't expect you to find something in these, I am just posting this to show how many bullshit error comes up by only adding one include in Header.h
« Last Edit: March 07, 2015, 01:14:25 pm by jilda »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Including header in a header - loop
« Reply #1 on: March 07, 2015, 01:29:43 pm »
You don't have header guards in both headers. You do in one but the other uses a #pragma once directive instead. Perhaps your tool-chain does not support that one. Try ditching the pragma and use header guards in both.
Regardless, it's usually best to structure your files such as to avoid include cycles. In many cases using forward declarations can help with this (and is also faster than including a full definition when it is possible).

*Edit*: One more thing. Identifiers containing double underscore "__" or starting with underscore followed by a upper-case letter are reserved for the implementation, so you really should choose different names for your header guard macros.
« Last Edit: March 07, 2015, 01:43:05 pm by Jesper Juhl »

jilda

  • Newbie
  • *
  • Posts: 5
  • dreaming in binary, living in real town
    • View Profile
Re: Including header in a header - loop
« Reply #2 on: March 07, 2015, 01:46:29 pm »
thanks, I think I got it working.
The change of header guards from #pragma once didn't work.
I added a forward declaration to Header.h
class Quadtree;
and included the Quadtree.h in Person.cpp

It seems to work. For now.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Including header in a header - loop
« Reply #3 on: March 07, 2015, 06:39:46 pm »
hmm that is odd.  Shouldn't need the forward declaration but you do need include guards.  I forget though when forward declarations are needed and when they aren't though so I could be wrong.
I have many ideas but need the help of others to find way to make use of them.

 

anything