I have been looking through a lot of different examples and explanations, but none has really answered what I am looking for. I have three classes with a method called connect for each one:
class foo { ... }
void foo::connect(bar br) { ... }
class bar { ... }
bar& bar::connect(baz bz) { ... }
class baz { ... }
baz& baz::connect() { ... }
In my main class, I 'connect' them like this:
foo.connect(bar);
bar.connect(baz);
baz.connect();
or:
foo.connect( bar.connect( baz.connect() ) );
(I know this is briefly explained, I can explain it better if needed)
So, I was trying to overload the operator '>>' to have something like this in the main function:
foo >> bar >> baz;
For the first operator it works, so if I simply do the following, it works fine:
foo >> bar.connect(baz.connect);
But, when I set the other '>>' operator g++ returns this error:
error: no match for ‘operator>>’ in ‘operator>>((* & foo), (* & bar)) >> baz.baz::connect()’
I think I'm not overloading the operator '>>' properly:
bar& operator>> (bar &br, baz &bz)
{
...
}
Thanks for the help :)
Operators aren't anything magical: they are just functions spelled in a funny way. That is, your statement
foo >> bar >> baz;
actually just calls [or tries to call]
operator>> (operator>> (foo, bar), baz);
That is, you need the operators
bar& operator>> (foo& f, bar& b) {
f.connect(b);
return b;
}
bar& operator>> (bar& b0, baz& b1) {
return b0.connect(b1);
}
Note, that the last connect()
you have won't be doable using operator>>()
because the operator always takes two arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With