You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求C++中usernameExist函数实现帮助:基于Account类的账户列表查重

Hey there! Let's work through this problem step by step. First, there's a small hurdle we need to clear before implementing the usernameExist function: your Account class has the username member marked as private, which means external functions like usernameExist can't directly access it. We have two common ways to fix this:


This follows encapsulation best practices by only exposing the data we need, rather than opening up full access to private members. First, update your Account class with a constructor (since you're instantiating objects with parameters) and a getter for the username:

#include <string>
using namespace std; // Or qualify with std:: if you prefer

class Account {
private:
    string name;
    unsigned int age;
    string username;
    string password;
public:
    // Constructor to initialize account values
    Account(string n, unsigned int a, string un, string p) 
        : name(n), age(a), username(un), password(p) {}

    // Getter to access the private username
    string getUsername() const {
        return username;
    }
};

Now we can implement the usernameExist function safely:

bool usernameExist(Account* userAccount, Account* accountList[], int numberOfElements) {
    // Grab the target username from the provided account
    const string targetUsername = userAccount->getUsername();

    // Iterate through every account in the list
    for (int i = 0; i < numberOfElements; ++i) {
        // Skip null pointers to avoid crashes
        if (accountList[i] != nullptr) {
            if (accountList[i]->getUsername() == targetUsername) {
                return true; // Match found!
            }
        }
    }

    return false; // No matching username found
}

Option 2: Use a Friend Function

If you prefer not to add a getter, you can declare usernameExist as a friend of the Account class to grant it direct access to private members:

#include <string>
using namespace std;

class Account {
private:
    string name;
    unsigned int age;
    string username;
    string password;
public:
    Account(string n, unsigned int a, string un, string p) 
        : name(n), age(a), username(un), password(p) {}

    // Declare the function as a friend to access private members
    friend bool usernameExist(Account* userAccount, Account* accountList[], int numberOfElements);
};

// Implement the friend function
bool usernameExist(Account* userAccount, Account* accountList[], int numberOfElements) {
    const string targetUsername = userAccount->username;

    for (int i = 0; i < numberOfElements; ++i) {
        if (accountList[i] != nullptr) {
            if (accountList[i]->username == targetUsername) {
                return true;
            }
        }
    }

    return false;
}

How to Use the Function

Here's a quick example of how to call usernameExist in your main program:

int main() {
    // Create your account instance
    Account first("Max", 18,"maxx34","12345adc");
    Account second("Luna", 20,"luna_99","moonlight123");

    // Create an array of Account pointers
    Account* accountList[] = {&first, &second};
    int numAccounts = sizeof(accountList) / sizeof(accountList[0]);

    // Check if Max's username exists in the list
    bool exists = usernameExist(&first, accountList, numAccounts);
    // `exists` will be true here
}

内容的提问来源于stack exchange,提问作者Gre3n

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:49:02