函数模板的C ++循环依赖性问题 [英] C++ circular dependency issue with function templates
问题描述
我在我正在开发的项目中有几个类;第一个是Solver类,最初具有完整定义在Solver头文件中的函数模板,如下所示(仅显示裸需求):
solver.h
class Solver {
public:
template< typename T&
void solve(T t);
}
template< typename T>
void Solver :: solve(T t){
//实现这里
}
b $ b
现在,类A被用作求解函数模板的模板参数,如下所示:
Ah
#includesolver.h
class A {
private:
Solver s; // s在实例化的构造函数中实例化
public:
void doSomething();
}
A.cpp
void A :: doSomething(){
s.solve< A&>(* this);
}
所以这是现在的罚款和dandy,的项目,我需要将solve()函数模板的定义移动到一个实现文件(solver.cpp)从头文件。根据我的理解,我可以做到这一点,只要我添加行明确地声明将使用的功能模板,如下所示的类型:
solver.cpp
template< typename T>
void Solver :: solve(T t){
//实现这里
}
template void Solver :: solve& );
但是,当我尝试编译解算器时,这不起作用,因为为了指定A类型我想在solve.cpp中用作模板参数,我需要有一个不是不完整的类型。但是A需要Solver来编译 - 所以我相信我有一个循环依赖。是否有任何方法可以解决这个问题?
我是相对较新的所有这一切,所以请容易在我请:) :)非常感谢。 >
Samoth几乎是正确的,你需要 class A;
)。但只有在使用它之前,不是在Solver类之前:
已编辑响应注释,您的最小代码示例太少了:)真正的问题是 标头卫士 :
#ifndef SOLVER_H_INCLUDED_
#define SOLVER_H_INCLUDED_
class Solver {
public:
template<类型名T>
void solve(T t);
};
#endif // SOLVER_H_INCLUDED_
和
// Ah
#ifndef A_H_INCLUDED_
#define A_H_INCLUDED_
#includeSolver.h
A类{
private:
Solver s; // s在实例化的构造函数中实例化
public:
void doSomething();
};
#endif // A_H_INCLUDED_
// Solver.cpp
#includeSolver.h
#includeAh
template< typename T>
void Solver :: solve(T t){
//实现这里
}
//显式实例化
template void Solver :: solve< int>(int);
// ...
template void Solver :: solve< A&>(A&);
这将工作
// main.cpp
#includeAh
int main()
{
A a;
a.doSomething();
}
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(); }
这篇关于函数模板的C ++循环依赖性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!