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.
When you have types that are this tightly coupled, the simplest solution is to put them in a single header file.
#pragma once
// forward declaration of A
struct A;
// declare B, since A needs it
struct B
{
A *a;
template<typename T>
void doThingB();
};
// now we can declare A
struct A
{
B b;
void doThingA() {}
};
// and finally, implement the parts of B that need A
// and need to be in the header
template<typename T>
void B::doThingB()
{
a->doThingA();
}
If you still want a B.h, then it can be a single line:
#include "A.h"
If you want to split A/B into multiple headers for your own organization, a simple solution is to add compile-time checks to ensure the files aren't included directly.
// A.h
#pragma once
#define A_IMPL
#include "B_impl.h"
#undef A_IMPL
// B.h
#pragma once
#ifndef A_IMPL
#error "B_impl.h can't be included directly; use A.h"
#endif
struct A;
struct B
{
A *a;
template<typename T>
void doThingB();
};
#include "A_impl.h"
template<typename T>
void B::doThingB()
{
a->doThingA();
}
// A_impl.h
#pragma once
#ifndef A_IMPL
#error "A_impl.h can't be included directly; use A.h"
#endif
struct A
{
B b;
void doThingA() {}
};
You can make the #ifdef checks more complicated if you want more of your headers to use the impl files directly, but the public interface remains blissfully unaware.
Based on Stephen's solution I came up with that architecture:
A.h
#pragma once
#define A_H
#include "B.h"
struct A
{
B b;
void doThingA() {}
};
#include "B_impl.h"
B.h
#pragma once
struct A;
struct B
{
A *a;
template <typename T>
void doThingB();
};
#ifndef A_H
#include "B_impl.h"
#endif
B_impl.h
#pragma once
#include "A.h"
template <typename T>
void B::doThingB()
{
a->doThingA();
}
main.cpp
#include "A.h"
int main() {}
So I can include A.h and B.h separately and also it looks cleaner for me.
Related
i have a problem to compile the classes with cyclic dependency, and i can not find the way to compile my code
the main problem is appeared in chain of classes that has dependency to each others
for example i have 6 header files(Classes) (A, B, C, D, E, F)
A included in E
F, D included in A
E included in F, D
now i have a loop and cant fix it
i simplify the problem then create the simple example to show what is my exact problem
A.h
#ifndef A_H
#define A_H
#include "B.h"
class A
{
public:
static A& getInstance()
{
static A instance;
return instance;
}
int i;
int sum()
{
return i+B::getInstance().j;
}
private:
A() {}
};
#endif
B.h
#ifndef B_H
#define B_H
#include "A.h"
class B
{
public:
static B& getInstance()
{
static B instance;
return instance;
}
int j;
int sum()
{
return j+A::getInstance().j;
}
private:
B() {}
};
#endif
main.cpp
#include "A.h"
#include "B.h"
#include <iostream>
int main()
{
A::getInstance().i=1;
B::getInstance().j=2;
int t1=A::getInstance().sum();
int t2=B::getInstance().sum();
std::cout<<t1<<std::endl;
std::cout<<t2<<std::endl;
return 0;
}
g++ main.cpp
In file included from A.h:3:0,
from main.cpp:1:
B.h: In member function ‘int B::sum()’:
B.h:17:12: error: ‘A’ has not been declared
return j+A::getInstance().j;
is there any way or solutions to resolve this?
If you can't use .cpp files for some reason, you can do this:
a.h:
#pragma once
class A {
public:
static A& getInstance();
int i;
int sum();
private:
A();
};
a_impl.h:
#pragma once
#include "a.h"
#include "b.h"
inline A& A::getInstance() {
static A instance;
return instance;
}
inline int A::sum() {
return i + B::getInstance().j;
}
inline A::A() {
}
b.h:
#pragma once
class B {
public:
static B& getInstance();
int j;
int sum();
private:
B();
};
b_impl.h:
#pragma once
#include "a.h"
#include "b.h"
inline B& B::getInstance() {
static B instance;
return instance;
}
inline int B::sum() {
return j + A::getInstance().i;
}
inline B::B() {
}
And then first include declarations a.h and b.h, and then implementations a_impl.h and b_impl.h:
#include "a.h"
#include "b.h"
#include "a_impl.h"
#include "b_impl.h"
#include <iostream>
int main() {
A::getInstance().i = 1;
B::getInstance().j = 2;
int t1 = A::getInstance().sum();
int t2 = B::getInstance().sum();
std::cout << t1 << std::endl;
std::cout << t2 << std::endl;
}
Now it will compile. In this particular example, B (or A) could've been implemented inside class definition (so, no b_impl.h). I separated declarations and definitions for both classes for the sake of symmetry.
I need to include two header files to each other but I have trouble doing this. Is there any way except using forward declaration and template to do this ? or I'm not allowed to do it in c++?
here is what I want to do:
// A.hpp file
#ifndef H_A_H
#define H_A_H
#include "B.hpp"
class A {
private:
vector<B*> b;
public:
void function() {
// using methods of B
}
};
#endif
// B.hpp file
#ifndef H_B_H
#define H_B_H
#include "A.hpp"
class B {
private:
vector<A*> a;
public:
void function() {
// using methods of A
}
};
#endif
You can't include two header files to each other. There should be forward declaration in one of the files and function definition has to be pushed to .cpp file where you can include the header file.
// HeaderA.h file
#ifndef H_A_H
#define H_A_H
#include "HeaderB.h"
class A {
private:
int b;
public:
void function() {
// using methods of B
B b;
b.function();
}
};
#endif
// HeaderB.h file
#ifndef H_B_H
#define H_B_H
class A;
class B {
private:
int a;
public:
void function();
};
#endif
// Main.cpp
#include "HeaderA.h"
#include "HeaderB.h"
void B::function()
{
// using methods of A
A a;
a.function();
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
You have a cyclic dependency. This answer explains how to deal with them via forward declarations.
This article also deals with cyclic dependencies.
If you 100% don't want to use forward declarations and it is possible you can split logic in a different class and use composition.
// SomeLogic.h
class SomeLogic
{
};
// A.h
#include "SomeLogic.h"
class A
{
SomeLogic someLogic;
};
// B.h
#include "SomeLogic.h"
class B
{
SomeLogic someLogic;
};
I have a class A with the following declaration (A.h file):
#ifndef __A_DEFINED__
#define __A_DEFINED__
class A
{
public:
template<typename T> inline void doThat() const;
};
#endif
and a class B deriving from that class (B.h file):
#ifndef __B_DEFINED__
#define __B_DEFINED__
#include <iostream>
#include "A.h"
class B : public A
{
public:
void doThis() const { std::cout << "do this!" << std::endl; }
};
#endif
So far, so good. My issue is that the function A::doThat() uses B::doThis():
template<typename T> inline void A::doThat() const { B b; b.doThis(); }
Usually, the circular dependency would not be an issue because I would just define A::doThat() in the .cpp file. In my case however, doThat is a template function so I can't do that.
Here are the solutions I have envisioned so far:
Defining the template function A::doThat() in a .cpp file. The issue with that is that I need to instantiate explicitly all the calls with various template arguments (there might be many in the real case).
After the declaration of the A class in A.h, add #include "B.h" and then define the A::doThat() function. This works fine in visual studio but g++ does not like it.
Is there a neat way to solve this problem?
EDIT: In the real case, there is not just one child class B, but several (B, C, D, etc.) The function A::doThat() depends on all of them. The function B::doThis() is also templated.
A default template parameter for the B class could work:
#include <iostream>
// include A.h
class B;
class A
{
public:
template<typename T, typename U = B> inline void doThat() const
{
U b; b.doThis();
}
};
// include B.h
class B : public A
{
public:
void doThis() const { std::cout << "do this!" << std::endl; }
};
// main
int main()
{
A a;
a.doThat<int>();
}
Usually the best way to allow a parent to call a child function is to declare the function as a pure virtual function in the parent and override it in the children.
#include <iostream>
class A
{
public:
virtual ~A() = default;
template<typename T> inline void doThat() const
{
// do some other stuff
doThis();
}
virtual void doThis() const = 0; // pure virtual function
};
class B: public A
{
public:
void doThis() const override
{
std::cout << "do this!" << std::endl;
}
};
int main()
{
B b;
A* ap = &b;
ap->doThat<int>();
}
The following does work with g++:
File A.h:
#ifndef __A_DEFINED__
#define __A_DEFINED__
class A
{
public:
template<typename T> inline void doThat() const;
};
#include "B.h"
template<typename T> inline void A::doThat() const { B b; b.doThis(); }
#endif
File B.h:
#include <iostream>
#include "A.h"
// We check for the include guard and set it AFTER the inclusion of A.h
// to make sure that B.h is completely included from A.h again.
// Otherwise the definition of A::doThat() would cause a compiler error
// when a program includes B.h without having included A.h before.
#ifndef __B_DEFINED__
#define __B_DEFINED__
class B : public A
{
public:
void doThis() const { std::cout << "do this!" << std::endl; }
};
#endif
File test_A.cpp:
// In this test case we directly include and use only A.
#include "A.h"
#include "A.h" // We test whether multiple inclusion causes trouble.
int main() {
A a;
a.doThat<int>();
}
File test_B.cpp:
// In this test case we directly include and use only B.
#include "B.h"
#include "B.h" // We test whether multiple inclusion causes trouble.
int main() {
B b;
b.doThat<int>();
b.doThis();
}
Alternative Idea:
I do not know whether you (or some coding conventions) insist on separate header files for each class, but if not the following should work:
You can put the definitions of class A and class B and of the member function template A::doThat<typename>() (in this order) together in one header file AandB.h (or whatever name you like).
This cries for polymorphism. There are two options using polymorphism:
Dynamic polymorphism, i.e. make A an abstract base class and call doThis() virtually:
struct A
{
virtual void do_this() const = 0;
template<typename T>
void doThat() const { doThis(); }
};
struct B : A
{
void doThis() const override { /* ... */ }
};
Of course, this only works if doThis() is not templated. If you need that, you could use
Static polymorphism, i.e. CRTP, when
template<typename Derived>
struct A
{
template<typename T>
void doThat() const { static_cast<const Derived*>(this)->template doThis<T>(); }
};
struct B : A<B>
{
template<typename T>
void doThis() const { /* ... */ }
};
If (as in your example code) B::doThis() is not called for the same object, but for some temporary, you could
template<typename typeB>
struct A
{
template<typename T>
void doThat() const { typeB b; b.template doThis<T>(); }
};
So i have 2 classes let's call it class A and B which inside of those classes have a reference for each other as a function's arguments. When i try to forward declare it like this:
// A.h (Header guarded)
namespace ns {
class B { // Attempt to forward declare B
public:
int getRand();
};
class A {
public:
float a, b;
void aFunc(B &b);
};
}
// B.h (Header guarded)
namespace ns {
class A { // Attempt to forward declare A
public:
float a, b;
};
class B {
public:
void bFunc(A &a);
int getRand();
};
}
The thing is, when i do this i got 'class' type redefinition error. I've been searching for the solution and still haven't came to a solution. Am i doing this right? Which i suppose i'm not, can you tell me where did i do wrong here?
They're not forward declaration, they're definition definitely.
You should
// A.h (Header guarded)
namespace ns {
class B; // forward declare B
class A {
public:
// Some functions with B references as arguments
};
}
And it's same for B.h.
According to your situation, there're just some member functions which take forward declared class as parameters, you can leave the declaration of member functions in .h file, and provide their definition in .cpp file. Such as
// A.h (Header guarded)
namespace ns {
class B; // forward declare B
class A {
public:
float a, b;
void aFunc(B &b);
};
}
// B.h (Header guarded)
namespace ns {
class A; // forward declare A
class B {
public:
void bFunc(A &a);
int getRand();
};
}
// A.cpp
#include "A.h"
#include "B.h"
namespace ns {
void A::aFunc(B& b) { /* ... */ }
}
// B.cpp
#include "A.h"
#include "B.h"
namespace ns {
void B::bFunc(A& b) { /* ... */ }
int B::getRand() { /* ... */ }
}
I have several classes in a project I'm working on; the first is a Solver class, originally with a function template whose full definition is in the Solver header file, like so (just showing the bare necessities):
solver.h
class Solver {
public:
template<typename T>
void solve(T t);
}
template<typename T>
void Solver::solve(T t) {
// implementation here
}
Now, class A is used as template parameter for the solve function template as follows:
A.h
#include "solver.h"
class A {
private:
Solver s; //s is instantiated in constructor
public:
void doSomething();
}
A.cpp
void A::doSomething() {
s.solve<A&>(*this);
}
So this is all fine and dandy as it is now, but for the purposes of the project, I need to move the definition of the solve() function template into an implementation file (solver.cpp) from the header file. As I understand it, I can do this as long as I add lines that explicitly state what types will be used with the function template, as follows:
solver.cpp
template<typename T>
void Solver::solve(T t) {
// implementation here
}
template void Solver::solve<A&>(A& a);
However this doesn't work when I try to compile solver, because in order to specify A as a type I want to use as a template parameter in solve.cpp, I need to have A not be an incomplete type. But A requires Solver in order to even compile - so I believe I have a circular dependency. Is there any way I can get around this issue?
I'm relatively new to all this, so take it easy on me please :) Much thanks.
Samoth is nearly right, you need class A; ("forward declaration"). But only before you use it, not before the Solver class:
Edited In response to comments, your minimal code sample was too minimal :) The real problem was Header Guards:
#ifndef SOLVER_H_INCLUDED_
#define SOLVER_H_INCLUDED_
class Solver {
public:
template<typename T>
void solve(T t);
};
#endif // SOLVER_H_INCLUDED_
And
// A.h
#ifndef A_H_INCLUDED_
#define A_H_INCLUDED_
#include "Solver.h"
class A {
private:
Solver s; //s is instantiated in constructor
public:
void doSomething();
};
#endif // A_H_INCLUDED_
// Solver.cpp
#include "Solver.h"
#include "A.h"
template<typename T>
void Solver::solve(T t) {
// implementation here
}
// explicit instantiations
template void Solver::solve<int>(int);
// ...
template void Solver::solve<A&>(A&);
This will work
// main.cpp
#include "A.h"
int main()
{
A a;
a.doSomething();
}
The best way to pass-by circular dependencies is to do this :
class A; // before the class Solver
class Solver {
public:
template<typename T>
void solve(T t);
}
template<typename T>
void Solver::solve(T t) {
// implementation here
}
What you can do is:
solver.h
#ifndef SOLVER_H_INCLUDED_
#define SOLVER_H_INCLUDED_
class Solver {
public:
template<typename T>
void solve(T t);
};
#include "solver.cpp"
#endif
solver.cpp
#include "solver.h"
template<typename T>
void Solver::solve(T t) {
// implementation here
}
and a.hpp
#ifndef A_H_INCLUDED_
#define A_H_INCLUDED_
#include "solver.h"
class A {
private:
Solver s; //s is instantiated in constructor
public:
void doSomething()
{
s.solve(*this);
}
};
#endif