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. error: no match for 'operator==' (operand types are 'Visitor' and 'const QString')

error: no match for 'operator==' (operand types are 'Visitor' and 'const QString')

Scheduled Pinned Locked Moved Unsolved General and Desktop
classobjectc++
5 Posts 4 Posters 9.7k 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 16 Oct 2022, 03:47 last edited by
    #1

    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();
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 16 Oct 2022, 05:55 last edited by
      #2

      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.

      A T 2 Replies Last reply 17 Oct 2022, 11:49
      3
      • C ChrisW67
        16 Oct 2022, 05:55

        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.

        A Offline
        A Offline
        Asperamanca
        wrote on 17 Oct 2022, 11:49 last edited by Asperamanca
        #3

        @ChrisW67
        Alternatively, you can provide std::find with a comparison function (e.g. a lambda) that accepts the needed parameters.

        1 Reply Last reply
        0
        • C ChrisW67
          16 Oct 2022, 05:55

          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.

          T Offline
          T Offline
          Tasha
          wrote on 19 Oct 2022, 02:55 last edited by
          #4

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

          J 1 Reply Last reply 19 Oct 2022, 05:15
          0
          • T Tasha
            19 Oct 2022, 02:55

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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 19 Oct 2022, 05:15 last edited by
            #5

            @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);
            

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

            1 Reply Last reply
            3

            1/5

            16 Oct 2022, 03:47

            • Login

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