I'm trying to make a std::vector
out of pointers to four operator overloading member functions. What's wrong with this:
struct Fractal
{ int dividee;
int divisor;
Fractal operator +(Fractal other)
{ [not important]
}
Fractal operator -(Fractal other)
{ [not important]
}
Fractal operator *(Fractal other)
{ [not important]
}
Fractal operator /(Fractal other)
{ [not important]
}
};
int main()
{ Fractal fractal = Fractal{3, 10};
typedef Fractal(*fractalOperator)(Fractal, Fractal);
std::vector<fractalOperator> ops =
{ &Fractal::operator +,
&Fractal::operator -,
&Fractal::operator *,
&Fractal::operator /
};
}
Compiler says
error: could not convert '{&Fractal::operator+, &Fractal::operator-, &Fractal::operator*, &Fractal::operator/}' from '<brace-enclosed initializer list>' to 'std::vector<Fractal (*)(Fractal, Fractal)>'
};
Which is not very helpful. What's the correct way? I'm using c++14.
Your typedef
is for a pointer to function which takes two Fractal
s and returns a Fractal
. What you want is a pointer to member function, which has a different syntax.
typedef Fractal(Fractal::*fractalOperator)(Fractal);
Or, with a using
alias, which I find easier to read
using fractalOperator = Fractal(Fractal::*)(Fractal);