我创建了一个自定义的rxcpp运算符validateImplementation
,该运算符应该只获取通用的可观察流,对进行一些验证,SimpleInterface
然后根据特定条件(在我的情况下为whatsMyId
)继续或结束该流
https://github.com/cipriancaba/rxcpp-examples/blob/master/src/SimpleOperators.cpp
template <class T> function<observable<T>(observable<T>)> SimpleOperators::validateImplementation(SimpleInterface component) {
return [&](observable<T> $str) {
return $str |
filter([&](const T item) {
if (component.whatsMyId() == "1") {
return true;
} else {
return false;
}
}
);
};
}
但是,当尝试使用中的validateImplementation
方法时main.cpp
,出现以下错误:
no matching member function for call to 'validateImplementation'
note: candidate template ignored: couldn't infer template argument 'T'
您能帮助我了解我在做什么错吗?
在C ++中,必须先完全解析类型,然后才能使用该函数。此外,只能从参数而不是返回类型推断模板参数。最后,带有模板参数的函数的定义在调用时(在标头中)必须是可见的,或者对于每种受支持的类型(在cpp中)必须显式实例化。
在这种情况下,我将避免显式实例化。这意味着有两个选择。
删除模板参数
function<observable<string>(observable<string>)> validateImplementation(SimpleInterface component);
将定义从cpp移到标头,并将main.cpp更改为关于类型的显式,因为无法推断出该类型。
o->validateImplementation<string>(s1) |
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句