C++ multiple definition errors in 3-way header include - c++

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

Related

Error "Unterminated conditional directive" in cross-referencing headers

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.

Including header file in header file

I have this code in C++:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "gspace.h"
class myclass {
public:
void update(gspace **);
}
gspace is another class defined in gspace.h. The compiler however is telling me:
include/myclass.h error: ‘gspace’ has not been declared|
Is there anything wrong in what I'm doing?
EDIT:
#ifndef GSPACE_H
#define GSPACE_H
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
class gspace
{
public:
gspace();
Assuming you have even declared a class named "gspace" in gspace.h I could think of two possible errors.
1) class gspace would not be in global scope i.e could be in some namespace.
2) Please check if header gaurds used in gspace not "MYCLASS_H".

Class Name does not declare type C++

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"
...

Undeclared Identifier vector of pointers to objects

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".

Forward declaration causing VC++ errors, not sure how to resolve

I've looked around for a while, tried quite a few different approaches to this issue, but still cannot seem to get past errors occurring from forward declaration in a program with 3 codependent classes.
Here's a abstracted view of my current code's structure, split into 6 files + 1 main program file:
File x.h:
#ifndef X_H
#define X_H
using namespace std;
class y;
class x
{
private:
y *m_oY;
public:
// constructors &c
};
#endif
File x.cpp:
#include "x.h"
#include "y.h"
// Fancy stuff...
File y.h:
#ifndef Y_H
#define Y_H
using namespace std;
class z;
class y
{
private:
z *m_oZ;
public:
// constructors &c
z *funcZ()
};
#endif
File y.cpp:
#include "y.h"
#include "z.h"
// Fancy stuff...
File z.h:
#ifndef Z_H
#define Z_H
using namespace std;
class z { ... };
#endif
File z.cpp:
#include "z.h"
// Fancy stuff...
File main.cpp:
#include "z.h"
#include "y.h"
#include "x.h"
#include <iostream>
using namespace std;
int main() { ... }
The first error I receive, trying to compile in VS with a clean, non-PCH, non-ATL project occurs in my implementation when trying to use Class z. The error tells me that it's using the definition of z from y.h, and I'm not sure how to remedy this without creating a circular include problem. Text of the error follows:
main.cpp(114) : error C2514: 'z' : class has no constructors
y.h(9) : see declaration of 'z'
Any clues as to what I'm doing wrong here?
This isn't a forward declaration problem. Main.c can see the full declaration of class z. It must be that z has no constructor, at least of the proper shape, or maybe it is private.