Below I have reproduced a simplified version of a part of my code that gives error when compiling.
testing.cpp
#include <iostream>
#include "../Beta.h"
#include "../Alpha.h"
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Alpha.h
#include <vector>
class Alpha
{
public:
typedef struct _info{
int k;
} info;
friend class Beta;
};
Beta.h
#include <vector>
class Alpha;
class Beta
{
public:
std::vector <Alpha::info*> vecInfo;
};
When I run g++ testing.cpp I get below error message
In file included from testing.cpp:10:0: ../Beta.h:8:15: error:
incomplete type ‘Alpha’ used in nested name specifier ../Beta.h:8:15:
error: incomplete type ‘Alpha’ used in nested name specifier
../Beta.h:8:27: error: template argument 1 is invalid ../Beta.h:8:27:
error: template argument 2 is invalid
I can see from the forum threads that the first error is because of some kind of cyclic dependency (Error: incomplete type used in nested name specifier). I am unable to see similarity between my code and their code. What am I doing wrong.
You need to include
Alpha.hinBeta.h, because the full class definition is needed to have access toAlpha::info. This will not create a cyclic dependency becauseAlpha.hdoes not includeBeta.h.