本文介绍了C ++麻烦将数据输入到私有向量(无效使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类帐户的向量。它是一个类BankingSystem的私人。

I have a vector of class "Account". It's private to a class BankingSystem. Here's how I have them defined.

帐户类别:

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类别:

BankingSystem Class:

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

我试图以这种方式在向量中存储新帐户。

I'm trying to store new accounts in the vector in this manner.

1)在BankingSystem.h中的addAccount函数

1) addAccount function in BankingSystem.h

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中创建帐户

2) createAccount in 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

3) storeAccount in BankingSystem.h

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

}

除了在向量中存储数据,一切都很好。行 accounts_.push_back(a); 有此错误; 在静态成员函数中无效使用成员accounts_。

Everything is working fine except storing data in the vector. The line accounts_.push_back(a); has this error; "invalid use of member 'accounts_' in static member function."

推荐答案

静态方法不能访问类实例( this )所以在 storeAccount addAccount 成员 accounts _ 不存在。

A static method does not have access to a class instance (no this) so inside of storeAccount and addAccount the member accounts_ does not exist.

FYI:在执行return语句后, $ c> cout<< \\\
\t帐户ID:<< a.accountID<<

FYI: nothing after a return statement will be executed so the line cout << "\n\t Account ID: " << a.accountID << " added successfully."; is rather useless in your current code.

考虑以下实现以供参考:

Consider the following implementation for reference:

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 ++麻烦将数据输入到私有向量(无效使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:49