samedi 23 mai 2020

Unable to use iterator to find a class object. (binary '==': 'userInfo' does not define this operatortor)

I am fairly new to C++ especially when it comes to using iterators with class vectors.

I am trying to create a function that would verify the user username/password on login through searching a vector that contains all the class objects (users) and matching it with the input. However, I am getting this error:

binary '==': 'userInfo' does not define this operator or a conversion to a type acceptable to the predefined operator

I was trying to find solutions online and I was told that that error is associated with an iterator being used incorrectly. Here's my function code:

void userInfo::VerifyUser()
{
    std::string tempusername = get_input < std::string >("CONSOLE: Please insert your username");
    std::vector<userInfo>::iterator user = std::find(m_allUsers.begin(), m_allUsers.end(), [&tempusername](userInfo& profile) {return profile.get_username() == tempusername; });
    {
        if (user != m_allUsers.end())
        {
            int choice = get_input<int>("CONSOLE: Username already exists - 0 to login and 1 to register");
            if (choice == 0)
            {
                std::string temppassword = get_input < std::string >("CONSOLE: Please insert your password to login");
                std::vector<userInfo>::iterator pass = std::find(m_allUsers.begin(), m_allUsers.end(), [&tempusername]( userInfo& profile) {return profile.get_username() == tempusername; });
                if (pass != m_allUsers.end())
                {
                    while (!(VerifyPassword(tempusername, temppassword)))
                    {
                        std::string temppassword = get_input < std::string >("CONSOLE: Please insert your password to login");
                        std::vector<userInfo>::iterator pass = std::find(m_allUsers.begin(), m_allUsers.end(), [&tempusername](userInfo& profile) {return profile.get_password() == tempusername; });
                    }

                    std::cout << "SUCESS: You have entered the correct information - logging you in." << std::endl;
                    userInfo profile(tempusername, temppassword, 1, 0);
                }
            }
            else if (choice == 1)
            {
                VerifyUser();
            }
        }
        else
        {
            std::cout << "CONSOLE: Username does not exist in the database - redirecting to registeration menu." << std::endl;
            userInfo().register_user();
        }
    }
}

bool userInfo::VerifyPassword(std::string username, std::string password)
{
    std::string tempuser;
    std::string temppass;
    std::string file_name = username + ".txt";
    std::fstream file(file_name);
    while (!file.eof())
    {
        file >> tempuser;
        file >> temppass;
    }
    if (temppass == password)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

EDIT: Full error output:

Severity    Code    Description Project File    Line    Suppression State
Error   C2446   '==': no conversion from 'const _Ty' to 'userInfo'  Database (Project 1)    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\xutility    4640    
Error   C2446   '==': no conversion from 'const _Ty' to 'userInfo'  Database (Project 1)    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\xutility    4640    
Error   C2676   binary '==': 'userInfo' does not define this operator or a conversion to a type acceptable to the predefined operator   Database (Project 1)    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include\xutility    4640    

All these errors point to this template function in xutlity file:

template <class _InIt, class _Ty>
_InIt _Find_unchecked1(_InIt _First, const _InIt _Last, const _Ty& _Val, false_type) {
    // find first matching _Val
    for (; _First != _Last; ++_First) {
        if (*_First == _Val) // Error C2676 {
            break;
        }
    }

    return _First;
}
  • I apologize for the lengthy amount of code! I'll try to create a minimal example!

Aucun commentaire:

Enregistrer un commentaire