Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

conversion from 'long' to 'const QJsonValue' is ambiguous



  • I want to insert certain unsigned data types into a QJsonObject, when I try this I get the message to the right of the line, black in a red background:

    conversion from 'type' to const QJsonValue' is ambiguous
    

    Where 'type' is actual type name. This occurs on:

    uint
    long
    ulong
    

    Is there anything I can do to fix this, each line looks almost the same with the only difference being the insertion of the type:

    objResponse.insert(clsModFileIO::mscszData, lngData);
    


  • @JonB , thank you, I'm a bit confused as I don't get the warning on:

    char
    uchar
    int
    short
    ushort
    

    But I do get the warning on:

    long
    uint
    ulong
    

    Looking on that page it details:

    QMetaType::Int
    QMetaType::UInt
    QMetaType::LongLong
    QMetaType::ULongLong
    QMetaType::Float
    QMetaType::Double
    

    As all having a destination type of QJsonValue::Double, but the warnings I'm seeing do not echo that.

    I've fixed the warnings by adding for each location that has this warning:

    qint64 int64Data = static_cast<qint64>(ulngData);                                        
    objResponse.insert(clsModFileIO::mscszData, int64Data);                                                                                
    


  • @SPlatten
    Look at the types accepted by QJsonValue constructor in https://doc.qt.io/qt-5/qjsonvalue.html. At a guess, yours match either int or qint64. So cast your value to whichever of the two you desire?



  • @JonB , thank you, I'm a bit confused as I don't get the warning on:

    char
    uchar
    int
    short
    ushort
    

    But I do get the warning on:

    long
    uint
    ulong
    

    Looking on that page it details:

    QMetaType::Int
    QMetaType::UInt
    QMetaType::LongLong
    QMetaType::ULongLong
    QMetaType::Float
    QMetaType::Double
    

    As all having a destination type of QJsonValue::Double, but the warnings I'm seeing do not echo that.

    I've fixed the warnings by adding for each location that has this warning:

    qint64 int64Data = static_cast<qint64>(ulngData);                                        
    objResponse.insert(clsModFileIO::mscszData, int64Data);                                                                                
    


  • @SPlatten
    I think, but don't quote me: all of your first 5 are "small" and promote/coerce to int automatically, and that's what the compiler will pick. For your second group of 3, the ones you are getting the error on, if you think about you can see the compiler can't be sure int will do it, while preserving sign/value correctly. They might all need qint64 to be sure, so that's why it's "ambiguous" between that and int. [Actually, I think you can quote me ;-) ]


Log in to reply