error: no match for 'operator==' (operand types are 'Visitor' and 'const QString')
-
I am trying to make it so that the vector is checked for the class object visitor->getName but it comes up with this error. (title)
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()); visitor->setTimeIn(QDateTime::currentDateTime()); visitorsList.push_back(*visitor); // Relevant code start if(std::find(visitorsList.begin(), visitorsList.end(), visitor->getFirstName()) != visitorsList.end()) { QMessageBox::warning(this, "Error", "Visitor already exists. Try again."); } // Relevant code end 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() << ", "; dataFile << visitorsList[i].getTimeIn().toString().toStdString() << std::endl; } dataFile.close(); QMessageBox::information(this, "Signed in", "Welcome " + ui->lineEditFirstNameIn->text()); delete visitor; // Clear the line edits lineEditFirstNameIn.clear(); lineEditLastNameIn.clear(); lineEditContact.clear(); lineEditPhone.clear(); lineEditDetails.clear();
-
std::find() compares items in provided range of an iterator for equality with the literal third argument. The items are Visitor objects and the third argument is a QString. Since there is no equality operator (i.e.
operator==()
) matching those two types you get the error message.If you are really looking to see if there is no existing Visitor that has the same first name as an existing Visitor then you could put a suitable operator==() in the Visitor class, something like this:
bool Visitor::operator==(const QString &firstName) { return (m_firstName == firstName); }
This addresses the error message, but i suspect that this is not what you actually want. Firstly, "Albert Einstein" and "Albert Schweitzer" are not the same visitor. Secondly, if the code is executed in the order listed, you have already added the record to the vector before checking for its prior existence i.e., you will always find a match.
-
@ChrisW67 Hey Chris, thanks for getting back to me so quickly. Unfortunately I tried what you said and it didn't register that even firstName was already in the vector.
Visitor.h
bool operator==(const QString &firstName);
Visitor.cpp
bool Visitor::operator==(const QString &firstName) { return (firstName == firstName); }
Dialog.cpp
Visitor *visitor = new Visitor(); if(std::find(visitorsList.begin(), visitorsList.end(), visitor->getFirstName()) != visitorsList.end()) { QMessageBox::warning(this, "Error", "Visitor already exists. Try again."); } visitor->setFirstName(ui->lineEditFirstNameIn->text()); visitor->setLastName(ui->lineEditLastNameIn->text()); visitor->setContact(ui->lineEditContact->text()); visitor->setPhone(ui->lineEditPhone->text()); visitor->setTimeIn(QDateTime::currentDateTime()); visitorsList.push_back(*visitor);
You were right about the error disappearing, but that's the only thing that happened - no error message popping up despite 2 names being in the same vector . Is there a way to do this without a lambda function?
-
@Tasha said in error: no match for 'operator==' (operand types are 'Visitor' and 'const QString'):
return (firstName == firstName);
Do you think this line makes any sense?
This line also does not make sense:if(std::find(visitorsList.begin(), visitorsList.end(), visitor->getFirstName())
You want to compare visitors, so you need a comparison function/method. You should implement this:
bool operator==(const Visitor &otherVisitor);