TheDarBear
TheDarBear

Reputation: 199

Include statement includes itself

I'm Working on a project in c++, but I am native to Java and have little c++ experience. the error i am having is that Cell and CellRenderer both include each other, but I have no idea how to fix this, as they both use one another. if I remove the #include, I get errors with cell, but if I keep it the errors disappear except for the Cell includes itself. This is my code:

#include <string>
#include <iostream>
#include <allegro5\allegro.h>
#include "Cell.h"
#include "Renderer.h"


using namespace std;


class CellRenderer: public Renderer{
Cell * cell;
ALLEGRO_BITMAP * image;
public:

CellRenderer(Cell * c)
{
    cell = c;
    image = cell->getImage();
}

void render(int x, int y)
{
    al_draw_tinted_scaled_bitmap(image, cell->getColor(),0,0,al_get_bitmap_width(image),al_get_bitmap_height(image),x-cell->getRadius(),y-cell->getRadius(),cell->getRadius()*2,cell->getRadius()*2,0);
}

bool doesRender(int x, int y, int wid, int ht)
{
    int cellX = cell->getX();
    int cellY = cell->getY();
    int radius = cell->getRadius();
    return cellX>x-radius&&cellX<x+wid+radius&&cellY>y-radius&&cellY<y+ht+radius;
}
}

class Cell{
public:
bool doesRender(int x, int y, int wid, int ht)
{
    return renderer->doesRender(x,y,wid,ht);
}

void render(int x, int y)//renders with center at x,y
{
    renderer->render(x,y);
}
};

any help would be greatly appreciated

Upvotes: 2

Views: 13438

Answers (3)

user2753089
user2753089

Reputation:

Tip for the larger related issues. Sometimes the spelling might be off and it can be troubling to see where it is setup incorrectly if you have a large project with many include files.

I find compiling one file at a time can identify where the include was setup incorrectly.

Upvotes: 0

Raju
Raju

Reputation: 1169

If you already have a header guard, please make sure that you didn't included the same header file in it by mistake.

Example

#ifndef EXAMPLE_H_
#define EXAMPLE_H_
.
.
.
#include Example.h   //It should be removed
.
.

#endif

Upvotes: 1

alexbuisson
alexbuisson

Reputation: 8469

You need to surround all header file you write with guard. There are 2 solutions to do that but only the 2nd will really works with all compilers.

  1. Visual Studio supports #pragma once. Put that on the 1st line of your header.

  2. All compiler have a preprocessor. Surround all the text in your header file with

      #ifdef ...
      #define ...
    
       other include, class declaration, etc...
    
      #endif
    

Replace the ... by a unique identifier for your file; for example, I often use as a convention:

 _filenameinlowercase_h_

Upvotes: 4

Related Questions