I have a interface for element that returns the mesh for the current element. I have mesh which is collection of element. The mesh also has reference to mesh because it want to manipulate mesh. When ever I try to compile I get error saying invalid use of incomplete type ‘class Mesh’.
AbsElem.h
class Mesh;
class AbsElem{
virtual Mesh& GetMesh()const = 0;
};
Elem1.h
#include "AbsElem.h"
#include <vector>
class Elem1 : public AbsElem{
Elem1(Mesh& m):my_mesh(m){}
virtual Mesh& GetMesh() const{return my_mesh;}
void DoStuff(){
my_mesh.GetVal();
}
Mesh& my_mesh;
};
Mesh.h
#include "Elem1.h"
class Mesh{
public:
int GetVal(){return 1;}
private:
std::vector<Elem1> collect_elem_;
};
main.cpp
#include "Mesh.h"
int main(){
Mesh m;
}
compile by : g++ main.cpp
compilation error :
In file included from Mesh.h:1, from main.cpp:1: Elem1.h: In member function ‘void Elem1::DoStuff()’: Elem1.h:10:4: error: invalid use of incomplete type ‘class Mesh’ my_mesh.GetVal(); ^~~~~~~ In file included from Elem1.h:1, from Mesh.h:1, from main.cpp:1: AbsElem.h:2:7: note: forward declaration of ‘class Mesh’ class Mesh; ^~~~