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

Is it possible to return an object of type QMap from a function call? It doesn't seem to like that I'm defining the function as a type QMap.



  • Hi. I'm very, very, very new to Qt. I'm modifying a huge codeset (C++ in Linux OS) and am wondering if it is possible to return an object of type QMap from a function call? It doesn't seem to like that I'm defining the function as a type QMap.

    Very simplified example:

    QMap doSomeFunction(int)
    {
    QString someStringName;
    QMap<QString, int> _someQMap;

    someStringName = "abc";
    someIntData = int + 1000;
    _someQMap.insert(someStringName, someIntData);
    return _someQMap;
    }

    void main
    {
    QMap <QString, int> _anotherQMap;

    _anotherQMap = doSomeFunction(2);
    return;
    }



  • QMap<QString, int> doSomeFunction(int)
    {
    QString someStringName;
    QMap<QString, int> _someQMap;
    
    someStringName = "abc";
    someIntData = int + 1000;
    _someQMap.insert(someStringName, someIntData);
    return _someQMap;
    }
    
    void main()
    {
    QMap <QString, int> _anotherQMap;
    
    _anotherQMap = doSomeFunction(2);
    return;
    }
    

    QMap<QString, int> doSomeFunction(int) /// <= do not forget to say which kind of qmap
    {

    }



  • @QTNewbie2020 QMap isn't, by itself, a complete type. A function can't return a QMap in-general. QMap<QString, int> is what you declare inside the function, so you have to declare the function as return that specific type of QMap to have a complete type. A QMap<float, int> for example would be a completely different type as far a sthe compiler is concerned. They just look like similar names to a human.


Log in to reply