Im trying to include a module within another module, but i can't compile due to the following error:
"Cannot build the following source files because there is a cyclic dependency between them: Module1.ixx depends on Module2.ixx depends on Module1.ixx."
I want to modClass1_ contain modClass2_ class and modClass2_ contain a pointer to static modClass1_.
Code i tried with success with C++17 Header and Source files (.h and .cpp)
// Class1.h
#pragma once
#include "Class2.h"
class modClass2_;
class modClass1_
{
public:
modClass1_() {};
~modClass1_() {};
int V = 2;
int getV() { return V; };
static modClass2_ mc2;
};
extern modClass1_ mc1;
// Class1.cpp
#include "Class1.h"
modClass1_ mc1;
modClass2_ modClass1_::mc2;
// Class2.h
#pragma once
#include "Class1.h"
class modClass2_
{
public:
modClass2_() {};
~modClass2_() {};
int V = 1;
int getV() { return V; };
int getClass1V();
};
// Class2.cpp
#include "Class2.h"
int modClass2_::getClass1V()
{
return mc1.V;
}
// Main.cpp
#include "Class1.h"
#include <iostream>
int main()
{
std::cout << mc1.getV() << "\n"; // gets modClass1_ V directly
std::cout << mc1.mc2.getClass1V() << "\n"; // gets modClass1_ V through modClass2_ through modClass1_
std::cout << mc1.mc2.getV() << "\n"; // gets modClass2_ V through modClass1_
}
Code i tried but failed with C++20 Modules (.ixx)
// Module1.ixx
export module Module1;
import Module2;
export class modClass1_
{
public:
modClass1_() {};
~modClass1_() {};
int getV() { return V; };
modClass2_ mc2;
int getModule2V() { return mc2.V; };
int V = 1;
};
export modClass1_ mc1;
// Module2.ixx
export module Module2;
import Module1;
export class modClass2_
{
public:
modClass2_() {};
~modClass2_() {};
int getV() { return V; };
int getModule1V() { return mc1.V; };
int V = 2;
};
Any help/suggestion will be appreciated.
Environment: Visual Studio 2019 | MSVC-2019 | C++20 | Windows 10 Pro
Class2.h
need to includeClass1.h
? To declare a pointer variable all you need is a forward declaration. – Prodrome#include "class2.h"
and class2.h has#include "class1.h"
. That's a circularity, and it simply won't work. This has nothing to do with modules. – Bianchi