代码之家  ›  专栏  ›  技术社区  ›  AntonyG

错误:C2039:“指针”:不是“QJsonObject::迭代器”的成员

  •  0
  • AntonyG  · 技术社区  · 8 年前

    当我尝试在MSVC2013中使用“QJsonObject::iterator”时,遇到了一个奇怪的错误。

    我有以下示例:

    #include <QCoreApplication>
    #include <QJsonObject>
    #include <QDebug>
    #include <algorithm>
    
    void processValue(QJsonValue value) {
        qDebug() << value.toString();
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QJsonObject jsonObject;
        jsonObject.insert("a", "A");
        jsonObject.insert("b", "B");
        jsonObject.insert("c", "C");
        jsonObject.insert("d", "D");
        jsonObject.insert("e", "E");
    
        std::for_each (jsonObject.begin(), jsonObject.end(), processValue);
    
        return a.exec();
    }
    

    这段代码在MSVC2008(交叉编译到WinCE)和MinGW上编译和运行,但在MSVC2013上则不然。在所有情况下,我都使用Qt 5.5.1。

    错误消息为:

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2039: 'pointer' : is not a member of 'QJsonObject::iterator'
            c:\qt\qt5.5.1msvc\5.5\msvc2013\include\qtcore\qjsonobject.h(96) : see declaration of 'QJsonObject::iterator'
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(584) : see reference to class template instantiation 'std::iterator_traits<_InIt>' being compiled
            with
            [
                _InIt=QJsonObject::iterator
            ]
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\algorithm(31) : see reference to function template instantiation 'void std::_Debug_range<_InIt>(_InIt,_InIt,std::_Dbfile_t,std::_Dbline_t)' being compiled
            with
            [
                _InIt=QJsonObject::iterator
            ]
            ..\QJsonObjectIteratorIssue\main.cpp(21) : see reference to function template instantiation '_Fn1 std::for_each<QJsonObject::iterator,void(__cdecl *)(QJsonValue)>(_InIt,_InIt,_Fn1)' being compiled
            with
            [
                _Fn1=void (__cdecl *)(QJsonValue)
    ,            _InIt=QJsonObject::iterator
            ]
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2146: syntax error : missing ';' before identifier 'pointer'
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2602: 'std::iterator_traits<_InIt>::pointer' is not a member of a base class of 'std::iterator_traits<_InIt>'
            with
            [
                _InIt=QJsonObject::iterator
            ]
            C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : see declaration of 'std::iterator_traits<_InIt>::pointer'
            with
            [
                _InIt=QJsonObject::iterator
            ]
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xutility(375) : error C2868: 'std::iterator_traits<_InIt>::pointer' : illegal syntax for using-declaration; expected qualified-name
            with
            [
                _InIt=QJsonObject::iterator
            ]
    

    我是否在这里做错了什么,这只是碰巧在另外两个编译器上工作?

    1 回复  |  直到 8 年前
        1
  •  3
  •   peppe    8 年前

    使用5.6或回传: https://code.qt.io/cgit/qt/qtbase.git/commit/?id=4a318a61824216ac499ff8b0b0c55dea90501005

    QJsonObject::(const_)迭代器:添加指针typedef

    否则,它们无法与std::算法或其他任何东西一起使用 需要iterator_traits。