无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)
Unable to use iterator to find a class object. (binary '==': 'userInfo' does not define this operatortor)
我对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);
}
}
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)