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

Author Topic: #include problem (more C++ related than just SFML)  (Read 3300 times)

0 Members and 1 Guest are viewing this topic.

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
#include problem (more C++ related than just SFML)
« on: March 11, 2009, 03:28:46 am »
In my program I'm trying to set up some kind of hierarchy.  A ResourceManager holds the game's resources (which includes Levels), Levels have an Update function which can add enemies to the game if certain criteria are met, and Enemies use the ResourceManager to set its image to its Sprite.  So if I do a #include for each required file, I end up getting an infinite recursive include loop (Visual Studio then throws "too many include files").  Now, if I use a #pragma once (which I had no idea what it was until this day...still don't rly know what it is) I get weird compiler errors (parts of my code that used to work before now dont and now cmath and list are breaking too).  How do I fix this?

EDIT: ok quick "Update:" I thought I had fixed the problem and deleted this thread; however, in my Level file if I comment out #include Enemy.h I do not get any errors; however if I uncomment it I get a plethora of weird erros:

Code: [Select]

Error 14 error C2143: syntax error : missing ';' before '{' C:\Program Files\Microsoft Visual Studio 9.0\VC\include\cmath 20 SFML Game
Error 20 error C2143: syntax error : missing ',' before '<' C:\Program Files\Microsoft Visual Studio 9.0\VC\include\list 20 SFML Game
Error 71 error C2143: syntax error : missing ';' before 'namespace' C:\Program Files\Microsoft Visual Studio 9.0\VC\include\iostream 12 SFML Game


Now I think it's because in my ResourceManager class I have a vector of Level pointers (to store the levels).  For some reason if I include Enemy in level, it breaks something.  Here are the includes in Enemy, Level, and ResourceManager, if that helps:
Code: [Select]

//Level.h
#pragma once
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>

#include "Enemy.h"


#ifndef LEVEL_H
#define LEVEL_H
...

//Enemy.h
#pragma once
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

#include "ResourceManager.h"
#include "Player.h"
...

//ResourceManager.h
#pragma once
#include <vector>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Level.h"




Another update, I moved most of the local include files into the cpp files.  It mostly has fixed it...until I need to actually put include files into headers.

tireswing

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • http://www.tireswinggames.com/
#include problem (more C++ related than just SFML)
« Reply #1 on: March 11, 2009, 07:21:37 pm »
I usually solve a lot of these recurive include problems by declaring a class without defining it.  For instance, your level class declaration may only need a pointer to your resource manager class.  In this case, you can create a pointer without including your resource manager header.

Example:

Level.h
------------------------
Code: [Select]
class ResourceManager;

class Level
{
  ....

  ResourceManager *m_pResourceMgr;
  ...
};


Now in ResourceManager.h, you are free to include Level.h

Astrof

  • Full Member
  • ***
  • Posts: 135
    • View Profile
#include problem (more C++ related than just SFML)
« Reply #2 on: March 11, 2009, 09:54:59 pm »
woah that's cool; I did not know I could do that.  Thanks!
C++ is still fairly new to me; I'm used to Java and C# where you import (or use) files but do not dump the whole include file into the current file; it just says "Hey I'm using blah blah blah and this is where it is defined."

 

anything