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

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.

Related

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

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

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

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

Namespace problems

So I am getting the following errors:
..\Actor.h:35: error: `Attack' is not a member of `RadiantFlux'
..\Actor.h:35: error: template argument 1 is invalid
..\Actor.h:35: error: template argument 2 is invalid
..\Actor.h:35: error: ISO C++ forbids declaration of `attacks' with no type
On this line (among others):
std::vector<RadiantFlux::Attack> attacks;
Here are the relevant files:
Actor.h:
#ifndef ACTOR_H_
#define ACTOR_H_
#include <string>
#include <vector>
#include "Attack.h"
namespace RadiantFlux {
...
class Actor {
private:
std::string name;
int health;
std::vector<RadiantFlux::Attack> attacks;
Attributes attributes;
public:
...
};
}
#endif /* ACTOR_H_ */
Attack.h:
#ifndef ATTACK_H_
#define ATTACK_H_
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Actor.h"
namespace RadiantFlux {
...
class Attack {
private:
...
public:
...
};
}
#endif /* ATTACK_H_ */
Why am I getting these errors and what can I do to fix them? I am assuming it has something to do with the namespaces...
You have a cyclic dependency of your header files.
Attack.h includes Actor.h and vice versa.
Use Forward Declaration of class to avoid circular dependency problems.
Since the OP's comments, here is what needs to be done:
class Actor;
class Attack
{
};
If your code fails to compile after doing this, You need to read the linked answer and Understand why the error and how to solve it. The linked answer explains it all.
The classes Actor and Attack both refer to each other, so you will need to add a forward declaration in one of the file.
For example, in Actor.h:
class Attack;
class Actor
{
...
};

Compiling Error with C++ and namespace

Here's the whole code getting the errors:
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include "DXManager.h"
namespace XEngine
{
class Engine
{
};
}
#endif
DXManager.h
#ifndef DX_MANAGER_H
#define DX_MANAGER_H
namespace XEngine
{
class Engine; // forward declaration
class DXManager
{
public:
void run(Engine *engine);
};
}
#endif
DXManager.cpp
#include <iostream>
#include "Engine.h"
#include "DXManager.h"
using namespace XEngine;
void DXManager::run(Engine *engine)
{
std::cout<<"DXManager::run"<<std::endl;
}
With these 30 lines of code, I'm getting 20 errors like:
'XEngine' : a namespace with this name does not exist
'XEngine' : a symbol with this name already exists and therefore this name cannot be used as a namespace name
syntax error : identifier 'Engine'
Clearly, I'm missing something important here. What am I doing wrong?
note: I am aware that circular dependency is a bad thing, but in my particular case I believe that it is relevant.
In DXManager.cpp you are not just using some names from namespace XEngine. You define the function in that namespace.
So must be:
DXManager.cpp
#include <iostream>
#include "Engine.h"
#include "DXManager.h"
namespace XEngine {
void DXManager::run(Engine *engine)
{
std::cout<<"DXManager::run"<<std::endl;
}
}
AFAIK some of the compilers (like MSVC) process using variant too.
But it is not correct because your syntax tries to define function ::DXManager::run - not ::XEngine::DXManager::run you intend to define.
In the forward-declaration of class Engine the namespace XEngine doesn't exist at this point.
A workaround would be moving the declaration inside the namespace block.
When Engine.h includes DXManager.h, the latter defines a class XEngine::Engine without declaring the namespace first.