I am trying to serialize a custom class I have made, given a pointer to an instance of the class. This code fails to compile because it can't resolve the operator<<(out, myObj)
.
QDataStream& operator<<(QDataStream &out, MyObj const *&m);
QDataStream& operator>>(QDataStream &in, MyObj *&m);
void MainWindow::serialize(QDataStream &out)
{
MyObj *myObj = new MyObj();
operator<<(out, myObj);
}
void MainWindow::deserialize(QDataStream &in)
{
MyObj *myObj = new myObj();
operator>>(in, myObj);
}
QDataStream &operator<<(QDataStream &out, MyObj const *&) { return out; }
QDataStream &operator>>(QDataStream &in, MyObj *&) { return in; }
The compile error is as follows:
MainWindow.cpp:79:33: error: call of overloaded 'operator<<(QDataStream&, MyObj*&)' is ambiguous
MainWindow.cpp:79:33: note: candidates are:
../Qt5.0.1/5.0.1/gcc_64/include/QtCore/qchar.h:395:28: note: QDataStream& operator<<(QDataStream&, QChar) <near match>
../Qt5.0.1/5.0.1/gcc_64/include/QtCore/qchar.h:395:28: note: no known conversion for argument 2 from 'MyObj*' to 'QChar'
...
Interestingly, the compiler only fails to find the first operator overload. I am able to fix this by using object references rather than references to object pointers, but I am curious why this will not compile.
Why would the compiler be unable to find the implementation of the first operator?