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