类型类的输出向量 [英] Outputting Vector of Type Class

查看:150
本文介绍了类型类的输出向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题中,此问题有更多代码: C ++麻烦将数据输入到私人媒体(无效使用)

There is more code to this question in this previous question: C++ Trouble Inputting Data into Private Vector (invalid use)

我尝试输出Account类型的向量

I'm trying to output a vector of type "Account"

帐户:

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

这是我声明向量的方式:
std :: vector< Account> accounts _;

This is the way I'm declaring the vector: std::vector<Account> accounts_;

这里是我打印到屏幕上的方法:

And here's how I'm trying to print it to the screen:

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

但我得到这个错误二进制表达式的无效操作数。

But I'm getting this error "invalid operands to binary expression".

当前代码;

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";
}
}


推荐答案

应为帐户类重载运算符< <在课程中:

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

在全局(或您的,您定义的帐户)命名空间

In global (or yours, where Account is defined) namespace

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

或在类中创建一些输出函数

or create some output function in class for example

void print(std::ostream& os) const { }

然后自由运算符<

std::ostream& operator << (std::ostream& os, const Account& acc)
{
    acc.print(os);
    return os;
}

这篇关于类型类的输出向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆