no match for 'operator>>' (operand types are 'QDataStream' and 'double')
Hello!
I would like to be able to serialize my custom vector class to a QDataStream, and for some reason that I don't understand, my operator overloading doesn't work.
Here is the relevant part of my vector class:
Code:
#include <QDataStream>
template <int N=3>
class NVec
{
double v[N];
public:
NVec() {memset(v, 0, N*sizeof(double));}
};
template <int N>
QDataStream& operator<<(QDataStream& out, const NVec<N> &o)
{
for (int i = 0; i < N; i++)
out << o[i];
return out;
}
template <int N>
QDataStream& operator>>(QDataStream& in, const NVec<N> &o)
{
for (int i = 0; i < N; i++)
in >> o[i];
return in;
}
The compiler complains with
Quote:
no match for 'operator>>' (operand types are 'QDataStream' and 'double')
even though I am certain that I have used QDataStream with doubles before. It is also strange that only the read operator fails, but not the write operator.
Can anyone please point out where I went wrong?
Thanks
Cruz
Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')
For operator>>(), the NVec<N> argument has to be non-const. (You're modifying it after all).
Cut-n-paste strikes again, eh?
Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')
Oh crap. I am the stupidest stupid in the world.
Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')
Nope, I've done it too. I was stupid before you were. ;)
Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')
You _were_, but now I have to wait for someone else to make that mistake in order to pass on the stupid. :)
Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')
Yeap, I've come to collect the stupid :) Feel free to pass it on :)
I have a similar problem here except the problem persists even after the argument was changed to a non-const. Any idea as to why that might happen? Below is the implementation of the two operators.
Code:
{
out << pid.getKp() << pid.getKi() << pid.getKd();
return out;
}
{
in >> pid.getKp() >> pid.getKi() >> pid.getKd();
return in;
}
Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')
Do the getter functions, e.g. PID.getKp(), return a reference to which the >> operator could write?
Cheers,
_
Re: no match for 'operator>>' (operand types are 'QDataStream' and 'double')
Yes that was the problem. Thank you. I changed it to the code below and now it's okay. I guess I'll wear my cap for while :) Thanks again.
Code:
{
double kp;
double ki;
double kd;
in >> kp >> ki >> kd;
pid.setKp(kp);
pid.setKi(ki);
pid.setKd(kd);
return in;
}