Error C2679 while trying to serialize QList of custom class with QDatastream

泄露秘密 提交于 2020-01-05 04:39:10

问题


I got a custom class Foo:

class Foo
{
public:
    // getters and setters
    // ...
private:
    QString string;
    QStringList list;
    int number;
}

The serialization of Foo has worked as expected:

QDataStream &operator<<(QDataStream &stream, const Foo &foo)
{
    stream<<foo.getString()<<foo.getList()<<foo.getNumber();
    return stream;
}

but when I attempted to serialize QList<Foo>, I got

error C2679: binary '<<' : no operator found which takes a right-hand operand of type “const Foo”

which directed to the corresponding Qt code (qdatastream.h):

template <typename T>
QDataStream& operator<<(QDataStream& s, const QList<T>& l)
{
    s << quint32(l.size());
    for (int i = 0; i < l.size(); ++i)
        s << l.at(i); // <--- compiler error here
    return s;
}

I know QList<T>::at() returns const T &, but I have no idea why it should fail to compile here. Please point out my error, I'd like to know how to fix this. Also, if I miss the necessary parts of code please do let me know. Thanks!

[Edit]: For reference, my environment is Qt 4.8.5 with MSVC2010/ Win 7 and I am using Qt creator.

Besides, this is how I serialize QList<Foo>:

QFile file("file.dat");
file.open(QIODevice::ReadWrite);
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_8);

out<<fooList; // It's the instant of QList<Foo> and it's not a pointer
//^^^^^^^^^^ compiler errors occurs as this line is added

[Last Edit]:

I finally figure out what's going on:

The operator overload that I defined to serialize Foo is written in a seperate .cpp file, where

QDataStream &operator<<(QDataStream &stream, const Foo &foo)
{
    stream<<foo.getString()<<foo.getList()<<foo.getNumber();
    return stream;
}

is inside foo.cpp.

The above code can be compiled but now I realize that it will never be used!

The serialization of QList<Foo> is implemented in another file notFoo.cpp, which is a "bigger class".

As I compile notFoo.cpp, only the definition and methods inside the header qdatastream.h could be found hence the error C2679 comes out, where:

template <typename T>
QDataStream& operator<<(QDataStream& s, const QList<T>& l)
{
    s << quint32(l.size());
    for (int i = 0; i < l.size(); ++i)
        s << l.at(i); // <--- compiler error here
    return s;
}

It shows that compiler doesn't know how to further serialze type Foo.

binary '<<' : no operator found which takes a right-hand operand of type “const Foo”

which means << of s << l.at(i); is not found (since I defined it in another .cpp file)

After I move the serialization method from Foo.cpp to notFoo.cpp, the code can finally be compiled.

[Conclusion]:

The compiler didn't find the function signature in the Foo.h since I define the stream operator inline within Foo.cpp.


回答1:


You need to declare the stream operator in the same header where you declare the serialized type:

// Foo.h
#include <QString>
#include <QDataStream>

class Foo
{
public:
    // getters and setters
    // ...
private:
    QString string;
    QStringList list;
    int number;
};

QDataStream &operator<<(QDataStream &stream, const Foo &foo);

Usually, the operator's implementation will be in the file where you implement Foo, but it could be in any .cpp file, really:

// Foo.cpp

Foo::Foo() {
  ...
}

...

QDataStream &operator<<(QDataStream &stream, const Foo &foo)
{
    stream<<foo.getString()<<foo.getList()<<foo.getNumber();
    return stream;
}


来源:https://stackoverflow.com/questions/22979932/error-c2679-while-trying-to-serialize-qlist-of-custom-class-with-qdatastream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!