无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)

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

本文关键字:userInfo 运算符 定义 二进制 迭代器 查找 对象      更新时间:2023-10-16

我对C++相当陌生,尤其是在将迭代器与类向量一起使用时。

我正在尝试创建一个函数,该函数将通过搜索包含所有类对象(用户(的向量并将其与输入匹配来验证登录时的用户用户名/密码。但是,我收到此错误:

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

我试图在网上找到解决方案,但我被告知该错误与迭代器使用不当有关。这是我的函数代码:

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;
}
}

编辑:完整错误输出:

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 Studio2019CommunityVCToolsMSVC14.25.28610includexutility    4640    
Error   C2446   '==': no conversion from 'const _Ty' to 'userInfo'  Database (Project 1)    C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.25.28610includexutility    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 Studio2019CommunityVCToolsMSVC14.25.28610includexutility    4640    

所有这些错误都指向 xutlity 文件中的这个模板函数:

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;
}
  • 对于冗长的代码,我深表歉意!我将尝试创建一个最小的例子!

您需要向userInfo类添加一个相等比较运算符:

class userInfo
{
public:
bool operator==(const userInfo& rhs) const
{
if (this == &rhs)
return true;
// rest of implementation
}
bool operator!=(const userInfo& rhs) const
{
return !(*this == rhs);
}
}