我有一类“帐户”的向量。它对一类BankingSystem来说是私有的。这是我如何定义它们。
帐户类别:
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
我正在尝试以这种方式将新帐户存储在引导程序中。
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中创建帐户
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);
}
除了将数据存储在向量中之外,其他一切工作正常。
accounts_.push_back(a);
行出现此错误; “在静态成员函数中无效使用成员'accounts_'。” 最佳答案
静态方法无权访问类实例(无this
),因此在storeAccount
和addAccount
内部的成员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++ - C++无法将数据输入到私有(private) vector 中(无效使用),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11908532/