C++模板类类型作为参数转发给对象的模板函数
好吧,我不知道标题是否有意义,所以我只展示代码:
template <class S,class P,class A> class Task
{
protected:
timeval start;
boost::ptr_vector<S> states;
boost::ptr_vector<P> policies;
Agent &robot;
const ACTION_MODE &a_mode;
A algo;
public:
Task(Agent &a, ACTION_MODE &m, A &alg) : robot(a), a_mode(m), algo(alg) {};
P* findPolicy(S *state);
bool stateExists(S *state);
bool if_appendState(S *state);
bool policyExists(P *policy);
bool if_appendPolicy(P *policy);
void run();
};
S类,用于状态(多态类),P类用于策略(模板化多态类),A类用于算法类。 例如(A类-类型):
class SarsaTD
{
protected:
const float gamma;
const float alpha;
PTYPE method;
public:
SarsaTD(float a, float g) : alpha (a), gamma (g) {method = ON_POLICY; };
template <typename P> void optimize(P *policy);
PTYPE getType();
};
我试图使用类Task的参数P(策略)并将其转发到SarsaTD实例的方法中。
template <class S,class P,class A> void Task<S,P,A>::run()
{
S *state = new S(Task<S,P,A>::robot, const_cast<ACTION_MODE&>(Task<S,P,A>::a_mode));
P *policy;
if (Task<S,P,A>::if_appendState(state))
{
policy = new P(state);
Task<S,P,A>::if_appendPolicy(policy);
}
else
{
policy = Task<S,P,A>::findPolicy(const_cast<S *>(state));
policy->getValue();
delete state;
}
Task<S,P,A>::algo.optimize<P>(policy);
wb_robot_step(TIME_STEP);
}
一切正常,直到行: Task
::algo.optimize
(policy);
编译器给出以下错误:
task.hpp:174: error: expected-primary expression before '>'token
如果我理解正确,我在整个模板类中使用的参数类型 P(策略)无法正确转发到模板函数中? 或者我的语法错误?或者我想做的事情根本没有意义?
Ok, I don't know if the title made much sense, so I will just show the code:
template <class S,class P,class A> class Task
{
protected:
timeval start;
boost::ptr_vector<S> states;
boost::ptr_vector<P> policies;
Agent &robot;
const ACTION_MODE &a_mode;
A algo;
public:
Task(Agent &a, ACTION_MODE &m, A &alg) : robot(a), a_mode(m), algo(alg) {};
P* findPolicy(S *state);
bool stateExists(S *state);
bool if_appendState(S *state);
bool policyExists(P *policy);
bool if_appendPolicy(P *policy);
void run();
};
Class S, is for States (a polymorphic class ) class P is for policies (a templated polymoprhic class) and class A is for Algorithm classes.
For example (a class A - type):
class SarsaTD
{
protected:
const float gamma;
const float alpha;
PTYPE method;
public:
SarsaTD(float a, float g) : alpha (a), gamma (g) {method = ON_POLICY; };
template <typename P> void optimize(P *policy);
PTYPE getType();
};
I am trying to use from class Task's parameter P (policies) and forward it into a method of an instance of SarsaTD.
template <class S,class P,class A> void Task<S,P,A>::run()
{
S *state = new S(Task<S,P,A>::robot, const_cast<ACTION_MODE&>(Task<S,P,A>::a_mode));
P *policy;
if (Task<S,P,A>::if_appendState(state))
{
policy = new P(state);
Task<S,P,A>::if_appendPolicy(policy);
}
else
{
policy = Task<S,P,A>::findPolicy(const_cast<S *>(state));
policy->getValue();
delete state;
}
Task<S,P,A>::algo.optimize<P>(policy);
wb_robot_step(TIME_STEP);
}
Everythign works fine, untill the line: Task<S,P,A>::algo.optimize<P>(policy);
Where the compiler gives the following error:
task.hpp:174: error: expected-primary expression before '>'token
If I understand this correctly, the Parameter type P (policy) I use throughout the template class cannot be forwarded properly into the template function ?
Or is my syntax wrong ? Or what I am trying to do simply makes no sense ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此时它无法知道 optimize 是否是一个值。如果你看看
这不仅看起来像是对你的比较,而且也是对编译器的比较,你就会明白。即使它继续进行解析,它也无法知道您的意图是比较还是模板参数列表,除非它完全实例化模板类型。如果您搜索“从属名称”和“两阶段查找”,您会找到一些有用的信息。您的情况的解决方案是手动消除歧义。告诉编译器
optimize
是一个模板:At that point it can't know whether
optimize
is a value or not. You'll understand if you just look atThis not only looks like a comparison to you, but also to the compiler. Even if it then proceeds with parsing, it can't know whether your intention was a comparison or a template argument list, unless it fully instantiates the template type. If you search for "dependent name" and "two phase lookup", you'll find some useful information. The solution in your case would be manual disambiguation. Tell the compiler that
optimize
is a template:您在表达式的另一部分使用模板参数,尝试这样做:
这应该像那样工作。
You use a template parameter in another part of the expression, try to do :
This should work like that.