gpt4 book ai didi

C++ 无法将数据输入私有(private) vector (无效使用)

转载 作者:行者123 更新时间:2023-11-28 00:55:05 25 4
gpt4 key购买 nike

我有一个“账户”类的 vector 。它对 BankingSystem 类是私有(private)的。以下是我对它们的定义。

账户类别:

struct newAccount
{
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;

}; //end of structure newAccount

class Account
{
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;

private:
int depositAmount;
int withdrawAmount;

public:
static newAccount createAccount( int, float, string, string, string ); //creates new account
void deposit( int ); //deposits money into account
void withdraw(int); //withdrawals money from account
int retdeposit() const; //function to return balance amount
friend class BankingSystem;

}; //end of class Account

银行系统类:

class BankingSystem
{
int accountID;
char fileName;

private:
std::vector<Account> accounts_;

public:
static void addAccount();
static void storeAccount( newAccount );
void deleteAccount();
void accountInquiry();
void saveAccounts();
void loadAccountsFromFile();
friend class Account;

}; // end of class BankingSystem

我正在尝试以这种方式在 vector 中存储新帐户。

1) BankingSystem.h中的addAccount函数

void BankingSystem::addAccount()
{
int ID;
float balance;
std::string pass, first, last;

cout << "\n\t Enter the Account ID: ";
cin >> ID;
cout << "\n\t Enter the passcode: ";
cin >> pass;
cout << "\n\t Enter Client's first name: ";
cin >> first;
cout << "\n\t Enter Client's last name: ";
cin >> last;
cout << "\n\t Enter starting balance: ";
cin >> setw(6) >> balance;

storeAccount( Account::createAccount( ID, balance, pass, first, last ) );

return;

}

2) Account.h中的createAccount

newAccount Account::createAccount( int ID, float balance, string first, string last, string pass )
{
newAccount a;
a.accountID = ID;
a.accountBalance = balance;
a.firstName = first;
a.lastName = last;
a.accountPass = pass;

return a;

}

3) BankingSystem.h中的storeAccount

void BankingSystem::storeAccount( newAccount a )
{
accounts_.push_back(a);

}

除了在 vector 中存储数据外,一切正常。 accounts_.push_back(a); 行有这个错误; “在静态成员函数中无效使用成员‘accounts_’。”

最佳答案

静态方法不能访问类实例(没有this)所以在storeAccount里面和 addAccount成员(member)accounts_不存在。

仅供引用:return 语句之后不会执行任何内容,因此行 cout << "\n\t Account ID: " << a.accountID << " added successfully.";在您当前的代码中相当无用。

考虑以下实现以供引用:

using namespace std;

class Account
{
private: // data members
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;

public:
// constructor that initializes members
Account(int id, float bal, const string& fname, const string& lname, const string& pass)
: accountID(id), accountBalance(bal), firstName(fname), lastName(lname), accountPass(pass) {}

}; //end of class Account

class BankingSystem
{
private: // data members
int accountID;
char fileName;
vector<Account> accounts_;

public:
void addAccount()
{
int ID;
float balance;
string pass, first, last;

// prompt input, initialize values, etc

// construct a new Account from values and add it to vector
accounts_.push_back(Account(ID, balance, first, last, pass));
}
void storeAccount( const Account& newAccount )
{
// add an already initialized account
accounts_.push_back(newAccount);
}


}; // end of class BankingSystem

关于C++ 无法将数据输入私有(private) vector (无效使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11908532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com