我有一个“帐户”类的向量。这是一个班级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
BankingSystem类:
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中的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);
}
除了在向量中存储数据外,一切正常。第accounts_.push_back(a);
行有此错误; “在静态成员函数中无效使用成员'accounts_'。”
答案 0 :(得分:3)
静态方法无权访问类实例(无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
答案 1 :(得分:0)
静态成员函数对accounts_
等成员变量没有任何特殊访问权限。
addAccount
和storeAccount
是静态成员函数。你必须出于某种原因制造它们,但这是一个错误。删除该静态,您将删除此错误。我猜你会有不同的问题。如果是这样,请问另一个问题并找出解决问题的正确方法。
答案 2 :(得分:0)
该方法是静态的,因此它没有“this”指针,所以它不知道你想要访问的accounts_变量是什么对象。此外,您永远不会在createAccount()上看到打印输出,因为它是在返回调用之后。