c++ - 类型类的输出 vector

标签 c++ vector

这个问题在上一个问题中有更多代码:C++ Trouble Inputting Data into Private Vector (invalid use)

我正在尝试输出“帐户”类型的 vector

账号:

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

private:
    int depositAmount;
    int withdrawAmount;

public:
    static Account 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

这是我声明 vector 的方式: std::vector<Account> accounts_;

下面是我尝试将其打印到屏幕上的方式:

for(int i=0; i < accounts_.size(); i++)
{     cout<< accounts_[i] <<endl;   }

但我收到此错误“二进制表达式的操作数无效”。

当前代码;

class BankingSystem
{
int accountID;
char fileName;

private:
std::vector<Account> accounts_;

public:
void addAccount();
void storeAccount( Account );
void deleteAccount();
void accountInquiry();
void saveAccounts();
void loadAccountsFromFile();
friend class Account;
friend std::ostream& operator << (std::ostream&, const Account&);

}; // end of class BankingSystem
#endif


std::ostream& operator << (std::ostream& os, const Account& acc)
{
// output members to os
return os;
}

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;

}

//function gets data from createAccount
void BankingSystem::storeAccount( Account newAccountToAdd )
{
//append to vector "accounts_"
accounts_.push_back(newAccountToAdd);

}

void BankingSystem::deleteAccount()
{
cout << "\nEnter The Account ID: ";
cin >> accountID;


}

void BankingSystem::accountInquiry()
{
int n;
cout << "\n\t Enter The Account ID (-1 for all): ";
cin >> n;

//cout << accounts_.size();

if (n == -1)
{
    cout << "\n\t List of all Accounts; (" << accounts_.size() << ") TOTAL: ";

    for(int i=0; i < accounts_.size(); i++)
    {     
        cout<< accounts_[i] << endl;   
    }
}
else
{
    cout << "\n\t Listing Account: " << n;
    cout << "\n\t I should search the vector for the ID you input";
}
}

最佳答案

您需要提供插入运算符:

std::ostream& operator<<( std::ostream& out, const Account& acct );

然后通过以适当的格式转储每个字段来在内部实现它。

关于c++ - 类型类的输出 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11909338/

相关文章:

c++ - 列表 vector 真的是 vector 吗?

c++ - 计算 vector 中的元素属性

c++ - 循环遍历 vector 时将对象添加到 vector

C++如何访问结构列表的字符串成员

c++ - 将 vector 拆分为多个数组/vector C++

java - java vector 方法set()和setElementAt()有什么区别?

c++ - 我的 Fwrite 无法工作,或者我的 FILE 在 Dev c++ 中无法工作

c++ - SSL_set_tlsext_host_name 崩溃

C++:合并排序和插入排序混合