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

Error in Converting Qstring to std::string



  • hi all and this is my code
    @{

        QString names;
        ofstream mys("e:/mystock.txt");
        mys.open("e:/mystock");
       names.toStdString();
        mys<<names;
        mys.close();
    

    }
    @
    when i change "names" variable to string there is no error
    but when i change it to qstring and then i want to change it to std string this error will come:

    no match for 'operator<<' (operand types are 'std::ofstream {aka std::basic_ofstream<char>}' and 'QString')
    mys<<names;
    ^

    Thanks


  • Moderators

    [quote]
    @
    names.toStdString();
    @
    [/quote]You need to use the return value of toStdString(). The function returns a copy of your data as a std::string, but it doesn't modify names. names is still a QString.



  • im very noob can you tell me how :| ?
    and thanks for your reply


  • Moderators

    You're welcome :)

    @
    mys << names.toStdString();
    @


Log in to reply