比较字符串对象等于向量中的元素

Compare string object equals to element in vector

本文关键字:向量 元素 字符串 对象 于向量 比较      更新时间:2023-10-16

所以我对C 的新手有些破坏。因此,我需要帮助。IM尝试在vector<Objects>中搜索String object,看看它们是否相等。正在搜索,我决定使用C 11 lamba表达式,它一直给我错误:

    Severity    Code    Description Project File    Line    Suppression State
Error   C3867   'User::getEmail': non-standard syntax; use '&' to create a pointer to member    EmailServerClientApp    c:usersuserdesktopemailserverclientappemailserverclientappguinterface.cpp 110 
    Severity    Code    Description Project File    Line    Suppression State
Error   C2678   binary '==': no operator found which takes a left-hand operand of type 'overloaded-function' (or there is no acceptable conversion) EmailServerClientApp    c:usersuserdesktopemailserverclientappemailserverclientappguinterface.cpp 110 

我创建了一个超载操作员(或至少据我了解我的知识)。看不到这怎么了。

这是我的用户类:

    private:
    string userName;
    string password;
    string email;
public:
    User();
    User(string name, string pass, string e);
    void setUserName(string name);
    void setPassword(string pass);
    void setEmail(string e);
    bool numberInString(const std::string& s);
    void print()const;
    User &operator=(User other)
    {
        std::cout << "copy assignment of Emailn";
        std::swap(userName, other.userName);
        std::swap(password, other.password);
        std::swap(email, other.email);
        return *this;
    }
    friend bool operator ==(const User &c1, const string &e);

    string getUserName()const;
    string getPassword()const;
    string getEmail()const;
    ~User();
};

创建的操作员是为了检查是否等于:

bool operator==(const User & c1, const string& e )
{
    return (c1.email == e);
}

和此方法,我正在尝试在向量中找到实际电子邮件:

bool checkIfUserExists(vector<User> v,string email, string password) {/* using c++11 lamba expression to find an 
                                                                      element in vector matching my string object 
                                                                      */
    vector<User>::iterator it = std::find_if(v.begin(), v.end(), [&email](const User&c1) {return c1.getEmail == email; });
        if (it != v.end())
        {
            return true;
        }
        else {
            return false;
        }
}

我在做什么错。请我需要帮助。很快就会哭泣。预先感谢

{return c1.getEmail == email;}

getEmail()是一种类方法,而不是类成员。正确的语法应为:

{return c1.getEmail() == email;}