Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Assign value to a string in a struct
Forum Updated to NodeBB v4.3 + New Features

Assign value to a string in a struct

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 958 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    russjohn834
    wrote on last edited by russjohn834
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • R russjohn834

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • R Offline
        R Offline
        russjohn834
        wrote on last edited by
        #3

        @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
        
        jsulmJ 1 Reply Last reply
        0
        • R russjohn834

          @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
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • R Offline
            R Offline
            russjohn834
            wrote on last edited by
            #5

            Thank you @jsulm. That got solved

            J.HilkJ 1 Reply Last reply
            1
            • R russjohn834

              Thank you @jsulm. That got solved

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @russjohn834
              Great!

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


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved