[英]template class specialization : template-id does not match any template declaration
I'm trying to use templates, but couldn't understand what is wrong with below code. 我正在尝试使用模板,但无法理解下面的代码有什么问题。
solve.h solve.h
#include "nlp.h"
#include "Ipopt_solve.h"
enum algo_type {IPOPT =1, SQP};
template<int ALG>
class solve
{
public:
solve()
{
}
};
template<>
class solve<IPOPT>
{
public:
solve(nlp*);
private:
Ipopt_solve m_ipopt;
};
solve.cpp solve.cpp
template<>
solve<IPOPT>::solve(nlp* problem): m_ipopt(problem)
{
}
Ipopt_solve
is a sub-class of an abstract class TNLP
. Ipopt_solve
是抽象类TNLP
的子类。 Ipopt_solve
is initialized with a reference to nlp
class. Ipopt_solve
通过引用nlp
类进行初始化。
from main.cpp 来自main.cpp
nlp problem(&model);
solve<IPOPT> solution(&problem);
I'm getting the error like shown below. 我收到如下所示的错误。
error: template-id 'solve<>' for 'solve<1>::solve(nlp*)' does not match any template declaration solve::solve(nlp* problem): m_ipopt(problem) 错误:'resolve <1> :: solve(nlp *)'的模板ID'解析<>'与任何模板声明都不匹配solve :: solve(nlp * problem):m_ipopt(problem)
You should remove template<>
, ie 你应该删除
template<>
,即
// template <>
solve<IPOPT>::solve(nlp* problem): m_ipopt(problem)
{
}
template<>
is used for template specialization (for a template); template<>
用于模板特化(对于模板); but you're just defining a non-template member function (of a class template specialization). 但是你只是定义了一个非模板成员函数(类模板特化)。 (That's why the compiler complains that the template declaration can't be found.)
(这就是编译器抱怨无法找到模板声明的原因。)
This declaration in its original form 该声明的原始形式
template<>
solve<IPOPT>::solve(nlp* problem): m_ipopt(problem)
{
}
is formally valid by itself. 是正式有效的。 However, it is not doing what you think it is doing.
但是,它没有做你认为它正在做的事情。 This declaration declares an explicit specialization for a member of the main template
此声明声明了主模板成员的显式特化
template<int ALG>
class solve
{
...
It has no relation to your explicit specialization 它与您的显式专业化无关
template<>
class solve<IPOPT>
{
...
The compiler is trying to specialize constructor solve<ALG>::solve(nlp* problem)
of the main template . 编译器正在尝试专门化构造函数
solve<ALG>::solve(nlp* problem)
主模板的 solve<ALG>::solve(nlp* problem)
。 However, the main template has no such constructor. 但是,主模板没有这样的构造函数。 Hence the error message, that tells you exactly that: the compiler does not understand what constructor you are trying to specialize, it cannot find the matching member in the main template.
因此错误消息,它告诉您确切的:编译器不理解您尝试专门化的构造函数,它无法在主模板中找到匹配的成员。
For example, you can use this syntax to explicitly specialize the default constructor of the main template 例如,您可以使用此语法显式专门化主模板的默认构造函数
template<>
solve<SQP>::solve()
{
// Specialized code for `solve<SQP>`'s default constructor
}
This will compile fine since the main template does indeed have such constructor. 这将编译正常,因为主模板确实有这样的构造函数。 (Note that you don't have to explicitly specialize the whole class for that, you can just explicitly specialize the constructor.)
(注意,您不必为此明确地专门化整个类,您可以只显式地专门化构造函数。)
Your intent was, obviously, completely different: to provide definition for the constructor in the class template specialization 显然,您的意图完全不同:在类模板特化中为构造函数提供定义
template<>
class solve<IPOPT>
{
...
The proper syntax for that should not mention template<>
正确的语法不应该提到
template<>
solve<IPOPT>::solve(nlp* problem): m_ipopt(problem)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.