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. QDateTime to a file (Invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'QDateTime')

QDateTime to a file (Invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'QDateTime')

Scheduled Pinned Locked Moved Solved General and Desktop
file handlingc++fstreamqdatetime
4 Posts 3 Posters 532 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.
  • T Offline
    T Offline
    Tasha
    wrote on 12 Oct 2022, 19:28 last edited by Tasha 10 Dec 2022, 19:30
    #1

    Hi there, I have a QdateTime and I'm trying to add it into a file, but I cannot because it contains Strings and Ints, so converting it to either or gives me an error: (Invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'QDateTime')

    My code below:

      std::vector<Visitor> visitorsList;
              Visitor *visitor = new Visitor();
                     visitor->setFirstName(ui->lineEditFirstNameIn->text());
                     visitor->setLastName(ui->lineEditLastNameIn->text());
                     visitor->setContact(ui->lineEditContact->text());
                     visitor->setPhone(ui->lineEditPhone->text());
    // ------------------ Relevant code ------------------
                     visitor->setTimeIn(QDateTime::currentDateTime());
    // ------------------ Relevant code end ------------------
    
                     visitorsList.push_back(*visitor);
    
                     std::fstream dataFile;
                     dataFile.open("data.txt", std::ios::app);
                     //Append visitorList vector  to file
                     for (size_t i = 0; i < visitorsList.size(); i++) 
    {
                         dataFile << visitorsList[i].getFirstName().toStdString() << ", " << visitorsList[i].getLastName().toStdString()  << ", " << visitorsList[i].getContact().toStdString()  << ", " << visitorsList[i].getPhone().toStdString();
    
    // ------------------ Relevant code ------------------
                 dataFile << visitorsList[i].getTimeIn()<< std::endl;
    // ------------------ Relevant code end ------------------
     }
                     dataFile.close();```
    
    Note: all the other visitorList[i].getXXX works just fine and is added to the data.txt file.
    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 Oct 2022, 19:31 last edited by
      #2

      @Tasha said in QDateTime to a file (Invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'QDateTime'):

      Hi there, I have a QdateTime and I'm trying to add it into a file,

      Convert it to a QString and then to a std::string.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply 12 Oct 2022, 19:43
      1
      • C Christian Ehrlicher
        12 Oct 2022, 19:31

        @Tasha said in QDateTime to a file (Invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'QDateTime'):

        Hi there, I have a QdateTime and I'm trying to add it into a file,

        Convert it to a QString and then to a std::string.

        T Offline
        T Offline
        Tasha
        wrote on 12 Oct 2022, 19:43 last edited by
        #3

        @Christian-Ehrlicher
        Hey ! Thank you so much for the help. Unfortunately, it only half works. The error is now gone, but it pops up with this instead:

        Expected ; before visitorsList (this error goes away if I add a ';' before getTimeIn.toString().toStdString but isn't useful because I need the getTimeIn )

        and

        Reference to overloaded function, did you mean to call it?

         dataFile << visitorsList[i].getFirstName().toStdString() << ", " << visitorsList[i].getLastName().toStdString()  << ", " << visitorsList[i].getContact().toStdString()  << ", " << visitorsList[i].getPhone().toStdString() << + " ";  visitorsList[i].getTimeIn().toString().toStdString() << std::endl;
        
        
        J 1 Reply Last reply 12 Oct 2022, 20:48
        0
        • T Tasha
          12 Oct 2022, 19:43

          @Christian-Ehrlicher
          Hey ! Thank you so much for the help. Unfortunately, it only half works. The error is now gone, but it pops up with this instead:

          Expected ; before visitorsList (this error goes away if I add a ';' before getTimeIn.toString().toStdString but isn't useful because I need the getTimeIn )

          and

          Reference to overloaded function, did you mean to call it?

           dataFile << visitorsList[i].getFirstName().toStdString() << ", " << visitorsList[i].getLastName().toStdString()  << ", " << visitorsList[i].getContact().toStdString()  << ", " << visitorsList[i].getPhone().toStdString() << + " ";  visitorsList[i].getTimeIn().toString().toStdString() << std::endl;
          
          
          J Offline
          J Offline
          JonB
          wrote on 12 Oct 2022, 20:48 last edited by
          #4

          @Tasha said in QDateTime to a file (Invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'QDateTime'):

          << + " ";

          Sort this bit out in your statement.

          1 Reply Last reply
          1

          1/4

          12 Oct 2022, 19:28

          • Login

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