PDA

View Full Version : no match for 'operator>>' (operand types are 'QDataStream' and 'double')



Cruz
8th December 2015, 15:37
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:



#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



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

d_stranz
8th December 2015, 15:56
For operator>>(), the NVec<N> argument has to be non-const. (You're modifying it after all).

Cut-n-paste strikes again, eh?

Cruz
8th December 2015, 15:59
Oh crap. I am the stupidest stupid in the world.

d_stranz
8th December 2015, 16:06
Nope, I've done it too. I was stupid before you were. ;)

Cruz
8th December 2015, 16:10
You _were_, but now I have to wait for someone else to make that mistake in order to pass on the stupid. :)

simex
5th January 2016, 01:22
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.


QDataStream &operator <<(QDataStream &out, const PID &pid)
{
out << pid.getKp() << pid.getKi() << pid.getKd();
return out;
}


QDataStream &operator >>(QDataStream &in, PID &pid)
{
in >> pid.getKp() >> pid.getKi() >> pid.getKd();
return in;
}

anda_skoa
5th January 2016, 12:18
Do the getter functions, e.g. PID.getKp(), return a reference to which the >> operator could write?

Cheers,
_

simex
5th January 2016, 14:44
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.

QDataStream &operator >>(QDataStream &in, PID &pid)
{
double kp;
double ki;
double kd;
in >> kp >> ki >> kd;
pid.setKp(kp);
pid.setKi(ki);
pid.setKd(kd);
return in;
}