创建用户数据库错误"no match for 'operator==' in....." --C++

Creating User database error "no match for 'operator==' in....." --C++

本文关键字:--C++ in operator match 用户数 用户 数据库 错误 创建 no for      更新时间:2023-10-16

我一直在命令提示符下处理用户数据库。它可以让你添加用户名和密码并登录。当我编译它时,我会得到错误no match for 'operator==' in。我不太确定是什么原因导致的。而且,我已经将所有这些都存储在一个类中。

我的头文件是:

#ifndef USER_PSW_H
#define USER_PSW_H
#include <string>

class User_Psw
{
    public:
        User_Psw();
        void addToDatabase();
        void getNameIndex();
        bool PasswordMatches();
        void UserCheck();
    protected:
    private:
        int sizeOfDatabase;
        int index;
        std::string Usernames;
        std::string Password;
        std::string username;
        std::string password;
};
#endif // USER_PSW_H

构造函数是:

User_Psw::User_Psw()
{
    const int SIZE = 100;
    index = 0;
    sizeOfDatabase = 0;
    Usernames[SIZE];
    Password[SIZE];
}

实际错误的函数为:

void User_Psw::getNameIndex()
{
    for(int i=0; i < sizeOfDatabase; i++)
    {
        if (username == Usernames[i])
        {
            index = i;
        }
    }
}

包含错误的实际代码行为if (username == Usernames[i])

如果需要,我还可以添加更多的代码片段。

这不起作用,因为当您实际将类型定义为std::string时,您访问Usernames就像访问向量一样。

你想要这样的东西:

std::vector<std::string> Usernames;

然后你可以访问它像:

if (Usernames[i] == username) { // etc