I have problem like there Why can templates only be implemented in the header file? (and there Correct way of structuring CMake based project with Template class) but with include recursion.
Code:
A.h
#pragma once
#include "B.h"
struct A
{
B b;
void doThingA() {}
};
B.h
#pragma once
struct A;
struct B
{
A *a;
template<typename T>
void doThingB();
};
#include "A.h"
template<typename T>
void B::doThingB()
{
a->doThingA();
}
main.cpp
#include "A.h"
int main() {}
Error:
In file included from A.h:2,
from main.cpp:1:
B.h: In member function 'void B::doThingB()':
B.h:16:6: warning: invalid use of incomplete type 'struct A'
16 | a->doThingA();
| ^~
B.h:2:8: note: forward declaration of 'struct A'
2 | struct A;
| ^
B.h
includes from A.h
but A.h
required by template function implementation in B.h
does not. I'm also cannot take implementation to an .cpp
because of template.
It's possible to solve by using explicit instantiation of templates but I'm wondering for another solution.