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

Assign value to a string in a struct



  • Hi all,

    I have basic c++ question:
    I have a following struct which need to be passed by a function:

     struct parsedData
        {
            std::string firstName;
            std::string surName;
            std::string patientID;
            std::string lastSession;
            std::string note;
            
        } newdata;
    

    How do I assign values to each string in the struct

    if I do this:

           if(xmlReader->name() == "Name") {
                    newdata.firstName = xmlReader->readElementText();
    }
    

    I'm getting error says no viable overloaded '='

    Could you please point out my mistake?


  • Lifetime Qt Champion

    @russjohn834 What is xmlReader here and what does readElementText return (I guess it does not return a std::string, so you need to convert)? Also you should post whole error message.



  • @jsulm

    xmlReader is a QxmlStreamReader, I do this:

    QXmlStreamReader *xmlReader;
     xmlReader = new QXmlStreamReader(xmlFile);
    
    // Parse each element of the XML until we reach the end.
        while(!xmlReader->atEnd() && !xmlReader->hasError()) {
            // Read next element
            QXmlStreamReader::TokenType token = xmlReader->readNext();
    
            // If token is just StartDocument - go to next
            if(token == QXmlStreamReader::StartDocument) {
                continue;
            }
    
            // If token is StartElement - read it
            if(token == QXmlStreamReader::StartElement) {
    
                if(xmlReader->name() == "Name") {
                    newdata.firstName = xmlReader->readElementText();
    
                } else if(xmlReader->name() == "Surname") {
                    newdata.surName = xmlReader->readElementText();
                }
                else if(xmlReader->name() == "Patient_ID") {
                    newdata.patientID = xmlReader->readElementText();
                }
                else if(xmlReader->name() == "Date") {
                    newdata.lastSession = xmlReader->readElementText();
                }
            }
        }
    

    error says this:

    patientdata.cpp:53:35: error: no viable overloaded '='
    xstring:925:8: note: candidate function not viable: no known conversion from 'QString' to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Myt' (aka 'basic_string<char, std::char_traits<char>, std::allocator<char> >') for 1st argument
    xstring:983:8: note: candidate function not viable: no known conversion from 'QString' to '::std::initializer_list<char>' for 1st argument
    xstring:1022:8: note: candidate function not viable: no known conversion from 'QString' to 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Myt' (aka 'const basic_string<char, std::char_traits<char>, std::allocator<char> >') for 1st argument
    xstring:1038:8: note: candidate function not viable: no known conversion from 'QString' to 'const char *' for 1st argument
    xstring:1043:8: note: candidate function not viable: no known conversion from 'QString' to 'char' for 1st argument
    

  • Lifetime Qt Champion

    @russjohn834 So, it returns a QString. Next step is to read https://doc.qt.io/qt-5/qstring.html where you will find https://doc.qt.io/qt-5/qstring.html#toStdString



  • Thank you @jsulm. That got solved


  • Moderators

    @russjohn834
    Great!

    Don't forget to use the topic tools to set this topic to solved 😉


Log in to reply