I have an hierarchy with an operator() overloading like the following
class Base{
public:
virtual std::vector<float> operator()(const std::vector<float>& us) const {
// some implementation like
// return us;
}
virtual float operator()(float u) const = 0;
};
and a derived class
class Derived : public Base{
public:
float operator()(float u) const override {
return u;
}
};
The invocation code looks like the code below (assume, the GetAVectorOfFloats second is indeed std::vector of floats).
Derived d;
auto [_, us] = GetAVectorOfFloats();
auto values = d(us);
However, the compiler (gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)) spits
error: no match for call to ‘(Derived) (std::vector&)’
I observe, that const std::vector<float>& is not an option (but why?).
In case the code is more explicit
auto values = d.operator()(us);
the error becomes
error: cannot convert ‘std::vector< float>’ to ‘float’
Seems, like I'm missing something very basic? Can not I call the Base's operator() or is there some special syntax I should use?
d.Base::operator()(us);should work. See demo.((Base&)d)(us)also works