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 thatzhas no constructor, at least of the proper shape, or maybe it is private.