不匹配运营商>>在QTCreator

时间:2016-02-02 20:10:45

标签: c++ qt pointers reference operators

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

经过一段时间后,我让第一个工作,不知何故,但我仍然得到一个错误,当我尝试在函数fromStream中使用第二个(>>),虽然我在下面预定义了它们。 错误是:

  

core / src / SearchObject.cpp:55:错误:不匹配'operator>>' (操作数类型是'QDataStream'和'SearchObject *')        out>>这;

我认为我的逻辑是错误的,我很困惑。

    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;
    }

1 个答案:

答案 0 :(得分:0)

在C ++中,this是指针类型SearchObject*(星号是#34;指针&#34;)。 operator>>期待引用类型SearchObject&(&符号为&#34;引用&#34;)。试试

out >> *this;
fromStream中的

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