I want to inline my two structures. But they have pointer of each other. Like the code below:
A.h
#ifndef A_H
#define A_H
#include "b.h"
struct A
{
B* b;
int value;
A();
void Inc();
};
#endif // A_H
A.cpp
#include "a.h"
A::A()
{
b = new B(this);
}
void A::Inc()
{
b->value++;
}
B.h
#ifndef B_H
#define B_H
struct A;
struct B
{
A* a;
int value;
B(A* a);
void Inc();
};
#endif // B_H
B.cpp
#include "b.h"
#include "a.h"
B::B(A *a)
{
this->a = a;
}
void B::Inc()
{
a->value++;
}
main.cpp
#include <iostream>
#include <a.h>
using namespace std;
int main()
{
A a;
a.value = 0;
a.b->value = 0;
a.Inc();
a.b->Inc();
cout << a.value << endl;
cout << a.b->value << endl;
return 0;
}
I can not use keyword inline
anywhere because it gives me the error undefined reference to methods
.
Based on here, I can define my methods in header files but that would force me to use #include <a.h>
inside the file b.h
. But that would give me another error because of duplicate includes.
Now How can I inline both Inc methods?