C++ Include guards - c++

I know this been asked a number of times but no answers seem to solve this.
I have two files.
Main.cpp
#include <irrlicht\irrlicht.h>
#include <vector>
#include <string>
#include <iostream>
#include "Scene.h"
#include "Camera.h"
#include "Gui.h"
irr::IrrlichtDevice* device;
irr::video::IVideoDriver* driver;
int main() {
device = irr::createDevice(irr::video::EDT_SOFTWARE, irr::core::dimension2d<irr::u32>(640, 480), 16, false, false, false, 0);
if (!device)
return 1;
device->setWindowCaption(L"NeoTrap");
driver = device->getVideoDriver();
sceneManager = device->getSceneManager();
GUIEnvironment = device->getGUIEnvironment();
//Testing
Mesh* ground = new Mesh();
//Testing
while (device->run()) {
driver->beginScene(true, true, irr::video::SColor(255, 120, 102, 136));
sceneManager->drawAll();
GUIEnvironment->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
Scene.h
#ifndef _SCENE_HEADER_
#define _SCENE_HEADER_
irr::scene::ISceneManager* sceneManager;
struct Mesh {
public:
Mesh();
private:
};
class Scene {
public:
Scene();
private:
};
#endif
What I am trying to do is declared a variable in Scene.h and define it from within the main function. I am not sure if I don't understand include guards, but I am getting weird errors:
'irr': is not a class or namespace name
syntax error: missing ';' before '*'
missing type specifier - int assumed. Note: C++ does not support default-int
but when I move the following line back in the Main.cpp file
irr::scene::ISceneManager* sceneManager;
the program compile. When am I not able to declare it in scene.h and set the value from the main function?

It's best to not declare variables in headers. It ends badly far more often than not because every file that includes the header will make their very own sceneManager. When the linker comes along to put the program together it may find dozens of sceneManagers all with an equal claim to being the real sceneManager, throw its hands up in disgust, and spray error messages all over the console.
In scene.h add
#include <irrlicht\irrlicht.h>
up at the top to declare all the bits and bobs of irrlicht so that they are available in scene.h.
Then change
irr::scene::ISceneManager* sceneManager;
to
extern irr::scene::ISceneManager* sceneManager;
extern tells the compiler that sceneManager exists and storage will be allocated somewhere else. The compiler smiles and carries on, leaving sorting out where the one, true sceneManager is to the linker.
Finally, put
irr::scene::ISceneManager* sceneManager;
in Main.cpp to allocate storage so that the linker has a sceneManager to find.
Documentation on extern
Recomended reading: When to use extern in C++

You are declaring that sceneManager is of type irr::scene::ISceneManager*, but the irr namespace doesn't exist when you are declaring that variable. Add an include to the header file that declares that namespace before declaring your variable.
After that, you'll need to declare sceneManager to be extern in the header so that each compilation unit that includes that header doesn't create its own instance of the variable. Then because it is extern, you will also want to redeclare it (without extern) in main.cpp.

Related

How to initialize all variables from a namespace in one class

I am making a small game using SFML.
I want to load all my resources in one go when the game launches, so every time a new entity is created new resources don't need to be loaded to memory slowing down the game.
I have made a header file with a namespace for each kind of resource I need:
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include "spritesheet.h"
namespace PlayerSprite {
extern Spritesheet playerSpritesheet;
extern sf::Sprite sp1, sp2, sp3, sp4, sp5, sp6;
extern int width = 16;
extern int height = 16;
}
namespace ButtonSprite {
extern Spritesheet buttonSpritesheet;
extern sf::Sprite button;
extern int width = 32;
extern int height = 16;
}
class Resources {
public:
Resources() = default;
void loadResources();
};
Now I don't know much about namespaces only very basic stuff, this is the first reason to use one.
Now in the cpp file:
#include <iostream>
#include "../headers/resources.h"
void Resources::loadResources() {
// PLAYER
PlayerSprite::playerSpritesheet.setSprite("./res/img/tiles.png");
PlayerSprite::sp1 = PlayerSprite::playerSpritesheet.getSprite(0, 0, PlayerSprite::width,
PlayerSprite::height);
PlayerSprite::sp2 = PlayerSprite::playerSpritesheet.getSprite(32, 0, PlayerSprite::width,
PlayerSprite::height);
PlayerSprite::sp3 = PlayerSprite::playerSpritesheet.getSprite(48, 0, PlayerSprite::width,
PlayerSprite::height);
PlayerSprite::sp4 = PlayerSprite::playerSpritesheet.getSprite(64, 0, PlayerSprite::width,
PlayerSprite::height);
PlayerSprite::sp5 = PlayerSprite::playerSpritesheet.getSprite(80, 0, PlayerSprite::width,
PlayerSprite::height);
PlayerSprite::sp6 = PlayerSprite::playerSpritesheet.getSprite(0, 16, PlayerSprite::width,
PlayerSprite::height);
// PLAYER
// BUTTONS
ButtonSprite::buttonSpritesheet.setSprite("./res/img/tiles.png");
ButtonSprite::button = ButtonSprite::buttonSpritesheet.getSprite(48, 16, 32, 16);
// BUTTONS
}
I then call this function in the main game file, but when I run I get this error in the player class:
undefined reference to 'PlayerSprite:sp1'
I don't know if this is because of the namespace declaration, or me coding the system wrong.
After looking at some comments on this question I have added this:
void Resources::allocateStorage() {
sf::Sprite PlayerSprite::spritesheet;
sf::Sprite PlayerSprite::sp1;
}
In the cpp file, but I am now getting this error:
unqualified-id in declaration before ';' token on the 1st line after the implementation of the function.
The problem seems to be that the variables are not actually defined anywhere, just declared. When using extern, the compiler will assume that this variable exists, so the compiler can continue on assuming that the variable exists. The linker will then come in, and see where that variable exists. If it does not, you get an undefined reference error. To fix this, you need to define the variables somewhere.
For example, in say resources.cpp, you would have:
#include "../headers/resources.h"
sf::Sprite PlayerSpriter::sp1;
sf::Sprite PlayerSpriter::sp2;
sf::Sprite PlayerSpriter::sp3;
// etc...
void Resources::loadResources() {
// …
}
This is in addition to what you already have in your header.
NOTE: From your edit, this would occur outside any function.

C++ Passing Reference of class to another class

I've been trying to pass my Graphics Manager class to both my Robot and Room class.
But when attempting to pass the class by reference I get 3 errors regarding the pass by reference.
These are the errors I'm referring to:
C2143 syntax error: missing ';' before '*'
C4430 missing type specifier - int assumed. Note: C++ does not support default-int
C2238 unexpected token(s) preceding ';'
I have attempted to change the way I've been passing the classes but with no luck, I have highlighted the areas in which is causing the error as well as the code that i have tried to use to fix the problem.
Any advice in how i could go about fixing these errors is highly appreciated.
I have not included the full .cpp files as they are quite large but I will include a link to a pasteBin with the full script.
GrapicsManager.h
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Room.h"
#include "Robot.h"
class GraphicsManager
{
public:
Room* room; //This does not Flag Up Errors
Robot* robot; //This does not Flag Up Errors
Robot.h
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System/String.hpp>
#include "GraphicsManager.h"
//#include "Room.h" //This what i had
class Room; //This is what i changed
//class GraphicsManager; //Wasnt sure if i should use it this
//way
class Robot
{
public:
//Graphics Variables
Room* room; //This works after the change
Robot* robot; //This works after the change
GraphicsManager *gm; //This throughs up the error
//This Is what i attemped to use with no effect
//GraphicsManager* gm = new GraphicsManager(room, robot);
Robot.cpp https://pastebin.com/Xd1A3Vii
#include "Robot.h"
Robot::Robot()
{
gm = new GraphicsManager(room, robot); //This tells me gm is
//not declared
this->room = room; //This does not flag up errors
this->robot = robot; //This does not flag up errors
//Room &room = *rm; // attempted to use this but decided not
//to
}
Room.h
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System/String.hpp>
#include "GraphicsManager.h" //
//#include "Robot.h" //what i orginally had
//class GraphicsManager; //i decided not to do it this way
class Robot; //What i changed it to
class Room
{
public:
//Reference to other classes
Room* room; //This doesnt throw errors
Robot* robot; //This doesnt throw errors
//Refference to graphics manager
GraphicsManager *gm; //This throws the three errors mentioned
};
Room.cpp https://pastebin.com/6R6vnVfy
#include "Room.h"
Room::Room()
{
gm = new GraphicsManager(room, robot);
this->room = room;
this->robot = robot;
It's the classic cicular include issue. GrapicsManager.h includes Room.h and Robot.h which each include GrapicsManager.h again. Now, for example, when compiling GraphicsManager.cpp you include GrapicsManager.h. But before you ever get to the GraphicsManager class definition, you first include Room.h. From there you go straight to include GrapicsManager.h again, but since you have a #pragma once in there, the compiler will simply skip that include. By the time the compiler then gets to the GraphicsManager *gm; member declaration in Room.h, is has never seen a declaration of a type named GraphicsManager. The error message that Visual C++ gives you then
C4430 missing type specifier - int assumed. Note: C++ does not support default-int
is arguably a bit unintuitive. At the point where it encounters the identifier GraphicsManager, an identifier can only mean the start of a declaration. Since GraphicsManager is not a known type, the compiler assumes that identifier must be the name of the entity that is supposed to be declared and you just forgot to specify the type. That's why you get the error message you see. C in the olden days used to allow you to omit the type specifier in a declaration which would just mean to use int as a default. So you would see this error as a result of trying to compile ancient, non-standard C code. That's why the error message contains the explicit note that that's not allowed…
You already added forward declarations for Room in Robot.h and for Robot in Room.h. You'll have to do the same for GraphicsManager…

C++ Thor library - problem with using resource loader class ( ' ' does not name a type)

I have been recently practicing managing multiple objects and drawing them in C++ using SFML library. I wanted my textures and future resources to be more reusable so I decided to make use of Thor library which suits my needs really well.
So I've written first few lines of code based on what you can find in this tutorial and the compiler always says:
main.cpp|12|error: 'textures_holder' does not name a type
This line gives an error :
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
I'm using Code::Blocks IDE with MinGW compiler and SFML 2.5.0.
Here's my main.cpp and the header file which contains extern object :
//...
#include <Thor/Resources.hpp>
#include "Dirt_Block.h"
using namespace std;
//Adding textures to the texture library
//THIS LINE GIVES AN ERROR
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//Rest of code...
Dirt_Block.h (only the upper part) :
#ifndef DIRT_BLOCK_H
#define DIRT_BLOCK_H
#include <SFML\Graphics.hpp>
#include <vector>
#include <Thor/Resources.hpp>
#include <Thor/Resources/SfmlLoaders.hpp>
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
//Rest of the code
I'd like to know what is causing this error and maybe help others who may experience similiar frustrating problems. Thanks for help.
EDIT :
As suggested in the comment I've declared a few extern int variables in the Dirt_Block.h so now it looks like this :
//...
extern int test_int_up;
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
extern int test_int_d;
//...
And then assinged to them some value in main.cpp :
//...
test_int_up = 55;
test_int_d = 55;
//Adding textures to the texture library
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//...
But the compiler gives error :
main.cpp|9|error: 'test_int_up' does not name a type
main.cpp|10|error: 'test_int_d' does not name a type
main.cpp|12|error: 'textures_holder' does not name a type
Much less distracting to see what your problem is without all the extraneous code!
C++ programs don't start from the top of the file and run code down to the bottom. They start at the main(), and control flow proceeds from there, with one thing triggering another.
(Note: That doesn't take into account global constructor ordering, which does go in order of declaration--but you have no guarantee of the order declarations from "different files" might run in.)
Point being, you can't just make random function or method calls in the middle of a file. That's where you put declarations. You have to be inside of a function or method to make calls, e.g.
int main() {
textures_holder.acquire(
"Dirt",
thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png")
);
...
}

C++ Issues creating class objects and circular inclusion

This is my first question, so I apologize in advance for any things about stack overflow that I may not be aware of!
The issue I am having is that I am making a simple SDL program, currently it is just supposed to draw a PNG image (Assets/Board.png) to the screen, but I am being faced with many errors to do with the two classes communicating with each other, I think that I have circular inclusion errors. But I have been trying to fix the problem for days and I haven't been able to solve it.
Here are the errors that I am getting:
http://imgur.com/gallery/vq3XLwU/new
(Here is a text version of it, but the formatting is bad sorry)
1>d:\code\c++\games\chess\chess\manager.h(41): error C2079: 'Manager::Tex' uses undefined class 'Render'
1>d:\code\c++\games\chess\chess\render.h(32): error C2146: syntax error : missing ';' before identifier 'manager'
1>d:\code\c++\games\chess\chess\render.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\code\c++\games\chess\chess\render.h(32): error C2146: syntax error : missing ';' before identifier 'manager'
1>d:\code\c++\games\chess\chess\render.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(I have used an image because the formatting of the image is easier to read.)
(Also click on the image in imgur to see the zoomed in version)
You can download the entire code project here (ZIP FILE):
https://www.mediafire.com/?og21315fc1d58sk
But here is the code that I think is causing the issues:
(Manager.h file)
//Include guards.
#pragma once
//Headers.
#include "Render.h"
//Librarys.
#include <SDL_image.h>
#include <string>
#include <SDL.h>
//Namespaces.
using namespace std;
//Enumerator.
enum State { Play, Exit };
class Manager
{
public:
Manager(); //Constructor.
~Manager(); //Destructor.
//Rendering.
SDL_Window* Window;
SDL_Surface* Screen;
SDL_Renderer* Renderer;
//Functions.
void Run();
void Init();
void Close();
void Input();
void Update();
void Error(string);
//Game variables.
State state;
Render Tex;
private:
//Constant values.
const int WINDOW_POS_X = SDL_WINDOWPOS_CENTERED;
const int WINDOW_POS_Y = SDL_WINDOWPOS_CENTERED;
const int INIT_FLAGS = SDL_INIT_VIDEO;
const int SCREEN_HEIGHT = 600;
const int SCREEN_WIDTH = 600;
};
(And here is the Render.h file)
//Include guards.
#pragma once
//Headers.
#include "Manager.h"
//Librarys.
#include <SDL_image.h>
#include <string>
#include <SDL.h>
//Namespaces.
using namespace std;
class Render
{
public:
Render(); //Constructor.
~Render(); //Destructor.
//Functions.
void Draw(int, int); //Draws texture at position.
void Load(string); //Loads texture from path.
void Deallocate(); //Destroy texture.
//Getter functions.
int GetHeight() { return Height; };
int GetWidth() { return Width; };
private:
SDL_Texture* Texture; //Actual image.
Manager manager; //Manager class.
int Height; //Height of image.
int Width; //Wdith of image.
};
So in conclusion, I am having errors related to the class objects, which I believe are being caused by circular inclusion, despite my research I have not been able to fix these issues. I would greatly appreciate any help with this issue.
P.S I know I am not supposed to use the std namespace, instead I must write std::foo, I am aware, so please don't comment about it!
In C++ "ownership" is a very important concept. A class "owns" its members. The problem is you have two objects and you've told the compiler that both "own" the other, which won't do. You have to decide which of them owns the other, and has it as a member (or maybe neither owns the other). At least one class must refer to the other, rather than listing it as a child/member. And the best sort of reference is a pointer.
class Manager;
// this forward declaration tell the compiler the class exists, but
// we don't care about the details yet, so we won't include the header.
// if we try to include it, that header refers to Render, which the compiler
// hasn't seen yet, so the compiler will fail.
class Render
{
public:
Render(Manager* parent_); //Constructor.
...stuff...
Manager* parent;
};
Cpp file:
#include "manager.h"
#include "render.h"
Render::Render(Manager* parent_)
: parent(parent_)
{}
SUGGESTIONS:
1. Eliminate the "Render.h" include:
There doesn't seem to be anything in "Manager.h" that needs anything in "Render.h" - so why include it?
Similarly, 2. Eliminate the "Manager.h" include.
You can simply use a "forward declaration", instead of including the whole class interface (.h file).
These links might help:
C++ circular include
Headers and Includes: Why and How
Okay, I think that I have solved the problem, thanks to a variety of comments. The solution may not be ellegant but it works. Instead of defining the Render Tex variabel inside the Manager header, I declared it inside the Manager.cpp class. Once I had done that I can #include "Render.h" inside the Manager .cpp file. I dont know if that explanation is clear but here is the top of the manager.h file:
//Include guards.
#pragma once
//Librarys.
#include <SDL_image.h>
#include <string>
#include <SDL.h>
And here is the top of the Manager.cpp file:
//Header.
#include "Manager.h"
#include "Render.h"
//Librarys.
#include <iostream>
//Objects.
Render Tex;

How can contents of a namespace be made available to an entire project without generating Multiply Defined Symbol errors?

I am attempting to make a fairly large video game, and my current method of handling mouse input is to pass mouseX, mouseY, and an enum mouseState as arguments to the update function of EVERY single object that requires knowledge of the mouse. It's fairly messy, and I want to make the mouse variables more global by putting them in a namespace called Input so that I can access them with Input::mouseX, et al.
As it stands now, The namespace Input exists in Input.h (contents below)
#pragma once
#include "allegro5\allegro.h"
#include "J_Enum.h"
namespace Input{
ALLEGRO_EVENT_QUEUE *inputQueue;
int mouseX;
int mouseY;
MOUSE_STATE mouseState;
void setUpInput();
void updateInput();
};
and the two member functions are defined in Input.cpp
#include "Input.h"
void Input::setUpInput(){...declaration
void Input::updateInput(){...''
Upon including Input.h in the main loop object's header Core.h the linker throws a hissy fit because in its eyes everything included in Input.h is now a Multiply Defined Symbol.
Clearly something is wrong with my use of header files, because to my knowledge, I haven't made any glaring mistakes in my use of namespaces and the error code prefix of LNK2005 seems to implicate the linker(?).
If anyone can possibly shed some light on my dilemma I would be most grateful
Declare the variables as extern:
// header file:
namespace Input {
extern int mouseX;
}
// implementation
#include "input.h"
namespace Input {
int mouseX;
}