2

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

TheDarBear
  • 199
  • 1
  • 1
  • 13

3 Answers3

4

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_
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alexbuisson
  • 7,699
  • 3
  • 31
  • 44
  • 1
    Actually `#pragma once` is [supported by most common compilers](http://en.wikipedia.org/wiki/Pragma_once#Portability), e.g. clang, g++, intel, and more. It is still true that you cannot rely on this functionality. And names in the global namespace that begin with an underscore [are reserved](http://stackoverflow.com/a/228797/1056003) and should not be used. – nikolas Aug 07 '13 at 07:06
  • @nijansen The problem with `#pragma once` is that the header might not be stored in one place. You might have multiple copies of the same header in multiple different directories or for multiple different compilers and so on. `#pragma once` is good for one file in one location but can get very messy and sometimes fail when there are multiple copies of the file in different locations being included. – Jack G Jun 27 '20 at 20:08
1

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
Raju
  • 1,149
  • 1
  • 6
  • 19
0

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.