I have two header files, animation.h and hero.h,
here is the code for animation.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
#ifndef ANIMATION
#define ANIMATION
//Class
#endif
And for hero.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
#ifndef HERO
#define HERO
//Class
#endif
I get the error message #include file "" includes itself even when using include guards.
I am new to using include guards and such so this may be a very trivial mistake.
Thanks for any help
You should put the header guards before anything else.
On gcc and MSCyou can also use
#pragma once
instead. Probably also on other more modern compilers. Simpliy put this at the top of your include, instead of the #ifdef....
animation.h
#ifndef ANIMATION
#define ANIMATION
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
//Class
#endif
hero.h
#ifndef HERO
#define HERO
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
//Class
#endif
Or animation.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
//Class
Related
This is a Qt project which, once built, results in a dll AnTS_Core.dll
so I have:
AnTs_Core.cpp
#include <windows.h>
#include "Globals.h" // my global values
extern "C"
{
__declspec(dllexport) void load();
}
void load()
{
mainDispatcher = new Dispatcher();
}
The Global header file which contains all main objects as global(because I want to call an object method from an other object):
Globals.h:
#ifndef GLOBALS_H
#define GLOBALS_H
#include "AnTS_Types.h"
#include "Dispatcher.h"
#ifdef __cplusplus
extern "C"
{
#endif
Dispatcher *mainDispatcher;
#ifdef __cplusplus
}
#endif
#endif // GLOBALS_H
The Dispatcher : header file
#ifndef DISPATCHER_H
#define DISPATCHER_H
#include "AnTS_Types.h"
#include "Device.h"
#include <list>
#include <windows.h>
class Dispatcher
{
public:
Dispatcher();
~Dispatcher();
private:
std::list<Device*> _devices;
};
#endif
Dispatcher.cpp :
#include "Dispatcher.h"
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string.h>
#include <dirent.h>
#include <regex>
#include "Device/DEV_Struct.h"
Dispatcher::Dispatcher()
{
}
and the device (Dispatcher contains a list of devices)
Device.h
#ifndef DEVICE_H
#define DEVICE_H
#include <windows.h>
#include "Device/DEV_Struct.h"
#include "AnTS_Types.h"
#define ANTS_DEVICE_NAME_LENGHT 64
class Device
{
public:
Device(char*);
~Device();
};
#endif // DEVICE_H
Device.cpp
#include "../Includes/Device.h"
#include <string.h>
#include <iostream>
#include <cstdio>
#include "Globals.h"
Device::Device(char* dllPath)
{
}
The errors are:
LNK2005 _mainDispatcher already defined in AnTS_Core.cpp.obj
LNK1169 one or more multiply defined symbols found
The errors disappear when I comment the line #include "Globals.h" in Device.cpp. But I want to access to global variables (to an other Dispatcher for example or to another object) from the device.cpp file.
So, this is a classical declaration vs definition issue - you have defined the variable mainDispatcher in the header and thus every compilation unit that includes this header winds up with a definition, where what you want is to declare the variable in the header as extern (this will only inform every compilation unit that includes the header that such variable exists):
#ifndef GLOBALS_H
#define GLOBALS_H
#include "AnTS_Types.h"
#include "Dispatcher.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern Dispatcher *mainDispatcher;
#ifdef __cplusplus
}
#endif
#endif // GLOBALS_H`
And you should place the actual definition Dispatcher* mainDispatcher in one of your .cpp files.
You have Dispatcher *mainDispatcher; in your Globals.h, this way every compilation unit which includes this header creates its own instance of this symbol. Declare extern Dispatcher *mainDispatcher; in Globals.h, and add Dispatcher *mainDispatcher; in AnTs_Core.cpp. This way you will have one symbol for AnTs_Core.cpp compilation unit, but others will see it through extern declaration.
I have 3 header files defining objects:
Point3d.h
#ifndef POINT3D_H
#define POINT3D_H
class Ray3d;
class Vector3d;
#include "Ray3d.h"
#include "Vector3d.h"
class Point3d {
...
};
#endif
Vector3d.h
#ifndef VECTOR3D_H
#define VECTOR3D_H
class Point3d;
class Ray3d;
#include "Ray3d.h"
#include "Point3d.h"
class Vector3d {
...
};
#endif
and Ray3d.h
#ifndef RAY3D_H
#define RAY3D_H
class Point3d;
class Vector3d;
#include "Point3d.h"
#include "Vector3d.h"
class Ray3d {
...
};
#endif
I won't include the .cpp files, but all the functions are defined there.
And then I have this class:
Transform.h
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <Eigen/Dense>
#include "../../geometry/Ray3d.cpp"
#include "../../geometry/Point3d.cpp"
#include "../../geometry/Vector3d.cpp"
using Eigen::MatrixXd;
class Transform {
...
};
#endif
AND FINALLY I have this subclass:
Translation.h
#ifndef TRANSLATION_H
#define TRANSLATION_H
//#include <Eigen/Dense>
#include "Transform.h"
//#include "../../geometry/Point3d.cpp"
//#include "../../geometry/Vector3d.cpp"
//#include "../../geometry/Ray3d.cpp"
using Eigen::MatrixXd;
class Translation : public Transform {
...
};
#endif
The problem is when I try to compile Translation.cpp:
g++ Transform.cpp Translation.cpp
I get a multiple definition of function error for every method in Ray3d, Point3d, and Vector3d. What can I do do avoid this? Should I be including less? Is my g++ command wrong? Thanks!
I'm also aware that I'm doing both forward declaration and includes in the first 3 headers, but that was the only way I could get those to compile. Part of the problem maybe?
You should not include the cpp files in transform.h
"but that was the only way I could get those to compile. Part of the problem maybe?"
You compile and link .cpp files separately, instead of including them (i.e. being seen from the preprocessor).
Your compiler command line should look rather like
g++ ../../geometry/Ray3d.cpp
../../geometry/Point3d.cpp
../../geometry/Vector3d.cpp
Transform.cpp Translation.cpp
-o MyExecutable
I am trying to compile my code, but I am getting an error with classes. One of the classes compiled fine(Example.h), but the other(Name.h) keeps giving me the class does not name type error. I think it has something to do with circular dependency, but how do I fix that without a forward deceleration?
Name.h
#ifndef _NAME
#define _NAME
#include <iostream>
#include <string>
using namespace std;
#include "Example.h"
class Name{
};
#endif
Example.h
#ifndef _EXAMPLE
#define _EXAMPLE
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
class Example{
};
#endif
I saw a post about using forward deceleration, but I need to access memebers from the Example class..
You have a circular dependency, where each header tries to include the other, which is impossible. The result is that one definition ends up before the other, and the name of the second is not available within the first.
Where possible, declare each class rather than including the entire header:
class Example; // not #include "Example.h"
You won't be able to do this if one class actually contains (or inherits from) another; but this will allow the name to be used in many declarations. Since it's impossible for both classes to contain the other, you will be able to do this (or maybe just remove the #include altogether) for at least one of them, which should break the circular dependency and fix the problem.
Also, don't use reserved names like _NAME, and don't pollute the global namespace with using namespace std;
see, here you are including #include "Example.h" in Name.h and #include "Name.h" in Example.h. suppose compiler compiles Name.h file first so _NAME is defined now, then it tries to compile Example.h here compiler wants to include Name.h but the content of Name.h will not be included in Example.h since _NAME is already defined, hence class Name is not defined inside Example.h.
you can explicitly do forward declaration of class Name; inside Example.h
Try this:
Name.h
#ifndef NAMEH
#define NAMEH
#include <iostream>
#include <string>
using namespace std;
//#include "Example.h"
class Example;
class Name{
};
#endif
Name.cpp
#include "Name.h"
#include "Example.h"
...
Example.h
#ifndef EXAMPLEH
#define EXAMPLEH
#include <iostream>
#include <string>
using namespace std;
//#include "Name.h"
class Name;
class Example{
};
#endif
Example.cpp
#include "Example.h"
#include "Name.h"
...
sorry in advance to start another one of those circular dependency threads but I tried almost everything and maybe a fresh pair of eyes can help.
How do I get this * compiled?
CARD.H
#ifndef CARD_H
#define CARD_H
#include <string>
#include <sstream>
#include <irrKlang.h>
#include "Database.h"
using namespace std;
using namespace irrklang;
class Card: public Database{ // problem expected class-name before '{' token
public:
DATABASE.H
#ifndef __DATABASE_H__
#define __DATABASE_H__
#include <string>
#include <vector>
#include <sqlite3.h>
#include <wx/string.h>
#include <irrKlang.h>
#include <wx/file.h>
#include "Card.h" // even though i include card.h
using namespace std;
using namespace irrklang;
class Card; // if i take this out, I get: 'Card' was not declared in this scope|
class Database
{
public:
vector<Card> queryC(wstring query);
Two rules to help prevent circular dependencies:
1.) If you don't need the implementation of a class, declare it by forward reference only.
2.) If you need the implementation, include the header file as late as possible.
Card.h
#ifndef CARD_H
#define CARD_H
#include "Database.h"
class Card : public Database {
public:
int card;
};
#endif // #ifndef CARD_H
Database.h
#ifndef DATABASE_H
#define DATABASE_H
#include <vector>
#include <string>
class Card;
class Database {
public:
std::vector<Card> queryC(std::string query);
};
#endif // #ifndef DATABASE_H
Card.cpp
#include "Card.h"
Card card;
Database.cpp
#include "Database.h"
Database database;
.
$ g++ -c Card.cpp -o Card.o
$ g++ -c Database.cpp -o Database.o
$ ls -l Card.o Database.o
-rw-r--r-- 1 user group 959 19. Sep 09:13 Card.o
-rw-r--r-- 1 user group 967 19. Sep 09:13 Database.o
Error: Line 12 of Cell.h: 'Actor' undeclared identifier.
If I try to forward declare above it, it says that there's a redefinition. What do I do?
Actor.h:
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;
class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();
};
#endif
Cell.h:
#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;
class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};
#endif
Cell.cpp:
#include "Cell.h"
#include <vector>
vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}
What else can I do?
Remove the #include "Cell.h" from Actor.h and you're set to go.
In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.
In the cpp files you can include the headers if you need them.
You have recursive #includes via your mutual references between cell.h and actor.h.
In Cell.h, delete #include <Actor.h>.
In Cell.h, add the line class Actor; just above the definition of class Cell.
In Cell.cpp, you might need to add #include "Actor.h".