我是C ++的新手,在使用2个运算符的预定义时遇到一些问题。

一段时间后,我以某种方式使第一个工作,但是当我尝试在fromStream函数中使用第二个(>>)时,尽管我在下面预定义了它们,但仍然出现错误。
错误是:


  core / src / SearchObject.cpp:55:错误:“ operator >>”不匹配(操作数类型为“ QDataStream”和“ SearchObject *”)
       出>>这个;


我想我弄错了逻辑或什么东西,我很困惑。

    void SearchObject::toStream(QDataStream in) {

        in << this;

    }


void SearchObject::fromStream(QDataStream out) {

    out >> this;

}


在这里,您可以看到更多代码:

    SearchObject::SearchObject() {

    }

    QDataStream& operator >>(QDataStream &in, SearchObject &searchObject) {



   //read object from stream

        QString medium;
        int mediumIndex;
        Annotation *ann = new Annotation("", "");
        QRect *roi;
        QString source;

        //skip opening bracket
        in.skipRawData(1);

        in >> medium;
        searchObject.setMedium(medium);

        //skip coma
        in.skipRawData(2);

        in >> mediumIndex;
        searchObject.setMediumIndex(mediumIndex);

        in.skipRawData(2);

        in >> *ann;
        searchObject.setAnnotation(ann);

        in.skipRawData(2);

        in >> *roi;
        searchObject.setROI(roi);

        in.skipRawData(2);

        in >> source;
        searchObject.setSourceDataset(source);

        in.skipRawData(1);

        return in;
    }

    QDataStream& operator <<(QDataStream &out, SearchObject &searchObject) {

        //write object to stream
        out << "(" << searchObject.getMedium() << ", "
            << searchObject.getMediumIndex() << ", "
            << searchObject.getAnnotation() << ", "
            << searchObject.getROI() << ", "
            << searchObject.getSourceDataset() << ")";
        return out;
    }

最佳答案

在C ++中,this是指针—键入SearchObject*(星号是“ pointer”)。 operator>>需要一个引用—键入SearchObject&(“引用”为“&”号)。尝试

out >> *this;


fromStream中(请注意this之前的星号)。 *thisthis指向的对象,因此应与SearchObject&匹配。

10-08 14:31