There are two classes that are related to each other in their headers:
PlotMarker
#ifndef PLOTMARKER_H
#define PLOTMARKER_H
#include <QObject>
#include "plotter.h"
class Plotter;
class PlotMarker : public QObject
{
// ...
Plotter* m_attachedPlot;
// ...
};
#endif // PLOTMARKER_H
Plotter
#ifndef PLOTTER_H
#define PLOTTER_H
// ...
#include "plotmarker.h"
// ...
class PlotMarker;
class Plotter : public QQuickPaintedItem
{
// ...
QLinkedList<PlotMarker*> m_markerList;
// ...
};
#endif // PLOTTER_H
The program is compiled well, but it's got a error error: unterminated conditional directive in #ifndef and the code of classes in the IDE isn't highlighted because of it.
If I remove #include "plotter.h" in PlotMarker's header or #include "plotmarker.h" in Plotter's header, Qt Creator highlights the code as usual, but the compilating fails because of errors about invalid use of incomplete type.
Could you please tell me what's wrong? I think it's because of wrong headers cross-referencing, but I ran into this and it didn't help me.
The problem is solved.
I just moved one of #include from the header to the source file, and it has worked.
plotmarker.h
#ifndef PLOTMARKER_H
#define PLOTMARKER_H
#include <QObject>
class Plotter;
class PlotMarker : public QObject
{
// ...
Plotter* m_attachedPlot;
// ...
};
#endif // PLOTMARKER_H
// ...
plotmarker.cpp
#include "plotmarker.h"
#include "plotter.h"
// ...
There is a fundamental design flaw.
For example
#include "b.h"
class A
{
B b; // B is an object, can't be forward declared
};
a.h header file above
#include "a.h"
class B {
A* a // A is an object, can't be forward declared
};
b.h header file above
This is a Circular Dependencies
The compiler will do the following:
#include "a.h"
// start compiling a.h
#include "b.h"
// start compiling b.h
#include "a.h"
// compilation of a.h skipped because it's guarded
// resume compiling b.h
class B { A* a }; // <--- ERROR, A is undeclared
I've just gotten "unterminated conditional directive" in #ifndef and "invalid preprocessing directive" in #end.
I've just added "if" after #end(edit "#end" to "#endif"), that error is fixed.
Related
My previous question was asked wrong, so I'll post it fixed.
I have this example throwing
expected class-name before ‘{’ token
error while compiling. I am understanding why is it fails, but I don't know how to fix it. Thank you.
BaseClass.h
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h"
class ElementClass;
class BaseClass
{
private:
ElementClass *m_someField;
};
#endif
ElementClass.h
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h"
class ChildClass;
class ElementClass
{
private:
ChildClass *m_class;
};
#endif
ChildClass.h
#ifndef INHERITTEST_CHILDCLASS_H
#define INHERITTEST_CHILDCLASS_H
#include "BaseClass.h"
class ChildClass : public BaseClass
{
};
#endif
You have circulary dependent .h files.
In BaseClass.h:
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h" // Includes ElementClass.h
In ElementClass.h:
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h" // Which included BaseClass.h
You can remove those #include lines since you are using those classes by pointers only and a forward declaration is sufficient for that purpose.
When you're working with inheritance the following
#include "ChildClass.h"
class ChildClass;
is unnecessary, if you're going to break these into sepperate source files (which it looks like you are) you can say
#include "ElementClass.h"
in the source file of your derived class
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 have a namespace defined in one header file and used in another, but it cannot be found. Specifically, a namespace called "players" defined in "players/Players.hpp" and used in a file called "players/Ownable.hpp" cannot be found in a file called "combat/Targetable.hpp"
The errors are
...\source\combat\Targetable.hpp(7): 'players' : is not a class or namespace name
...\source\combat\Targetable.hpp(7): 'Ownable' : base class undefined
Obviously it's some syntax thing I don't understand. I've spent some time simplifying the code so it looks silly, but bear with me.
// source/players/Players.hpp:
#ifndef PLAYERS_HPP
#define PLAYERS_HPP
#include "../Headers.hpp"
namespace players {
class Player{
// this class compiles fine.
// There used to be a "Players.cpp" but it's been simplified away
public:
int getID(){ return 0; }
int getTeam(){ return 0; }
string getName(){ return ""; }
Vec3 getColor(){ return Vec3(0.0,0.0,0.0); }
};
}
#endif
And players/Ownable.hpp, which is in the same folder as Player.hpp and also compiles fine:
// source/players/Ownable.hpp:
#ifndef OWNABLE_HPP
#define OWNABLE_HPP
#include "Players.hpp"
namespace players {
class Ownable;
typedef boost::shared_ptr<Ownable> OwnablePTR;
typedef boost::weak_ptr<Ownable> OwnableWPTR;
class Ownable {
public:
Ownable(){}
Ownable(int playerID) : playerID(playerID){}
bool isAlliedWith(OwnablePTR other){ return false; }
private:
int playerID;
};
}
#endif
Here's where the fun starts. I have a file at "source/combat/Targetable.hpp", which is in a different directory than the other two. However, the file itself seems to include fine:
// source/combat/Targetable.hpp:
#ifndef TARGETABLE_HPP
#define TARGETABLE_HPP
#include "../players/Ownable.hpp"
namespace combat{
class Targetable : public players::Ownable { // ERROR
public:
Targetable(int playerID){}
//Targetable(players::Player player);
virtual Vec2 getPosition(){
return Vec2();
}
virtual Vec2 getVelocity(){
return Vec2();
}
};
}
#endif
I'm really hoping this is some silly syntax thing that I'm missing. I've even tried
using players::Ownable;
but that A) pollutes the files that include this one, and B) doesn't fix anything. Any help?
EDIT: GManNickG got it, it was a circular include in the Headers.hpp file. Thanks!
You have a circular include.
First consider the purpose of include guards:
// a.hpp
#ifndef A_HPP
#define A_HPP
// stuff
#endif
// b.hpp
#ifndef B_HPP
#define B_HPP
#include "a.hpp"
// stuff
#endif
// c.hpp
#ifndef C_HPP
#define C_HPP
#include "a.hpp"
#include "b.hpp"
// stuff
#endif
// x.cpp
#include "c.hpp"
The inclusion of c.hpp will end up include a.hpp twice. The first time, the guards are not defined and everything is okay, and the second time the guards prevent redefinitions. This is what we want.
This does not work when you have a loop, though. (It will prevent it, which is good, but it does "too well" because the guard is defined right after its tested, which means the contents of the header haven't actually yet been processed). Consider this instead:
// a.hpp
#ifndef A_HPP
#define A_HPP
#include "c.hpp"
// stuff
#endif
// b.hpp
#ifndef B_HPP
#define B_HPP
#include "a.hpp"
// stuff
#endif
// c.hpp
#ifndef C_HPP
#define C_HPP
#include "b.hpp"
// stuff
#endif
// x.cpp
#include "c.hpp"
Which is similar to what you have. x.cpp includes c.hpp, which is the first time it's been included so it defines C_HPP. Then c.hpp includes b.hpp, which includes a.hpp. Then a.hpp includes c.hpp and finds that C_HPP has already been defined, so the include does nothing.
Assuming a.hpp still manages to compile (that is, c.hpp isn't actually needed), then a.hpp finishes, then b.hpp finishes, then c.hpp finally actually defines its contents before returning to x.cpp.
The solution is to minimize the amount of headers you include. Use forward declarations, and most of all: do not use 'include everything' headers! These are terrible. And I suspect that's what Headers.hpp is.
How about class Targetable : public ::players::Ownable { . . .?
Note the global namespace qualification :: before players.
So, I sort of expect this to end up being a simple answer, but I've been hacking at it for a while now, and can't seem to fix this issue. So I have a particular class, Intersection, that when included in any other header gives me:
error C2061: syntax error : identifier 'Intersection'
This is my Intersection header:
#ifndef INTERSECTION_H
#define INTERSECTION_H
#include "Coord.h"
#include "Road.h"
#include "TrafficLight.h"
class Intersection {
private:
int id;
Coord * midPoint;
Road * northRoad;
Road * eastRoad;
Road * westRoad;
Road * southRoad;
TrafficLight * trafficLight;
public:
Intersection(int, Coord *, Road *, Road *, Road *, Road *);
~Intersection();
void transitionTrafficLight();
int getId();
Road * getNorthRoad();
Road * getEastRoad();
Road * getWestRoad();
Road * getSouthRoad();
TrafficLight * getTrafficLight();
};
#endif
Now, if I attempt to use this class elsewhere, I get the error. For example:
#ifndef ROAD_H
#define ROAD_H
#include "Coord.h"
#include "Intersection.h"
#include <string>
class Road {
public:
enum LaneCount { TWO_LANE = 2, FOUR_LANE = 4 };
Road(std::string, Coord *, Coord *, LaneCount, Intersection *, Intersection *, int);
//shortened
Particularly at the Road constructor (and any other classes which reference Intersection). I don't think it's a syntax problem, as Coord is another class, defined in the same manner, and the compiler (VS 2008) doesn't complain about it. It's just Intersection in particular that's giving me this trouble. :/
I'm tagging it homework -- it's what it's for, even though this is just an error I can't get rid of rather than part of the problem.
Thoughts?
It looks like the error is that you have two header files that are circularly including one another - intersection.h and road.h. Doing this tends to lead to weird surprises in C++ because of how include guards work. For example, suppose that I have two header files that look like this:
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
and
// File: B.h
#ifndef B_Included
#define B_Included
#include "A.h"
class B {};
void MyOtherFunction(A argument);
#endif
Now, if I try to #include "A.h", then it expands out to
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
When I try expanding out the #include "B.h", I get this:
// File: A.h
#ifndef A_Included
#define A_Included
// File: B.h
#ifndef B_Included
#define B_Included
#include "A.h"
class B {};
void MyOtherFunction(A argument);
#endif
class A {};
void MyFunction(B argument);
#endif
At this point, the preprocessor will again try expanding out A.h, which leads to this:
// File: A.h
#ifndef A_Included
#define A_Included
// File: B.h
#ifndef B_Included
#define B_Included
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
class B {};
void MyOtherFunction(A argument);
#endif
class A {};
void MyFunction(B argument);
#endif
Now, let's see what happens when we resolve all of these weird include guards. The first time we see A, it's expanded out, as is the case when we expand out B for the first time. However, when we see A for the second time, it's not expanded out at all. Thus, after taking out comments and preprocessor directives, we get this resulting code:
class B {};
void MyOtherFunction(A argument);
class A {};
void MyFunction(B argument);
Notice that when MyOtherFunction is declared, A has not yet been declared, and so the compiler reports an error.
To fix this, you can forward-declare A and B in the header files that need them:
// File: A.h
#ifndef A_Included
#define A_Included
class A {};
class B; // Forward declaration
void MyFunction(B argument);
#endif
and
// File: B.h
#ifndef B_Included
#define B_Included
class B {};
class A; // Forward declaration
void MyFunction(B argument);
#endif
Now, there are no more circular dependencies. As long as you #include the appropriate header files in the .cpp files, you should be fine.
Hope this helps!
I have one class, called A, and it has it's own header file. Then I have another class, called B, which also has it's own header file. They each have their own .cpp file where I implement all of their functions.
I'm trying to have class B have a variable of class type A as a private variable, but I keep getting the error 'A' does not name a type
My code looks like this:
main.h:
#ifndef MAIN_H
#define MAIN_H
#include "A.h"
#include "B.h"
#endif
main.cpp:
#include "main.h"
int main( int argc, char* args[]) {
B test;
}
A.h:
#ifndef A_H
#define A_H
#include "main.h"
class A {
public:
//public functions
private:
//private variables
};
#endif
B.h:
#ifndef B_H
#define B_H
#include "main.h"
class B {
public:
//public functions...
private:
A temp;
}
#endif
So all of my includes are in main.h, which includes A before B. B has a variable of type A, but it is included from being in main.h and B.h includes main.h. However, I keep getting an error saying:
error: 'A' does not name a type.
I've done some googling, and it seems like that means that A isn't defined when you use it, but it should be defined there since it's being included in main.h, right?
The problem is that A.h includes main.h, which includes B.h, which tries to use A.
The good way to organize your files would be this:
main.h:
// not needed
main.cpp:
#include "B.h" // for using class B
int main( int argc, char* args[]) {
B test;
}
A.h:
#ifndef A_H
#define A_H
// no includes needed ATM
class A {
//...
};
#endif
B.h:
#ifndef B_H
#define B_H
#include "A.h" // for using class A
class B {
//public functions...
}
#endif
That way, B.h is self-contained and can be used without having to include anything else before it. That's very important as soon as your project grows above the toy level it is at now. Why would anyone trying to use what header x.h provides need to know to also include f.h, m.h, and u.h?
The code you provide compiles properly if you add a ; at the end of B.h
A better way of doing it would be #include "A.h" in "B.h", instead of #include "main.h"
But it is probably unrelated to your problem.
That kind of error may also be confusing if you are using templates and forget "typename".
A.h includes Main.h at the top.
Main.h skips A.h because A_H is already defined, then includes B.h.
B.h tries to make use of A, but A.h hasn't finished compiling yet so the type isn't defined.