输出类型类的向量
Outputting Vector of Type Class
在
上一个问题中,这个问题有更多的代码: C++ 将数据输入私有向量时遇到问题(无效使用)
我正在尝试输出"帐户"类型的向量
帐户:
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_;
这是我尝试将其打印到屏幕上的方式:
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 << "nt Enter the Account ID: ";
cin >> ID;
cout << "nt Enter the passcode: ";
cin >> pass;
cout << "nt Enter Client's first name: ";
cin >> first;
cout << "nt Enter Client's last name: ";
cin >> last;
cout << "nt 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 << "nt Enter The Account ID (-1 for all): ";
cin >> n;
//cout << accounts_.size();
if (n == -1)
{
cout << "nt List of all Accounts; (" << accounts_.size() << ") TOTAL: ";
for(int i=0; i < accounts_.size(); i++)
{
cout<< accounts_[i] << endl;
}
}
else
{
cout << "nt Listing Account: " << n;
cout << "nt I should search the vector for the ID you input";
}
}
您需要提供插入运算符:
std::ostream& operator<<( std::ostream& out, const Account& acct );
然后通过以适当的格式转储每个字段在内部实现它。
你应该为Account
类重载operator <<
。在课堂上:
friend std::ostream& operator << (std::ostream&, const Account&);
在全局(或定义帐户)命名空间中
std::ostream& operator << (std::ostream& os, const Account& acc)
{
// output members to os
return os;
}
或者在类中创建一些输出函数,例如
void print(std::ostream& os) const { }
然后free-operator <<
std::ostream& operator << (std::ostream& os, const Account& acc)
{
acc.print(os);
return os;
}
相关文章:
- C++中函数的向量返回类型引发错误
- 具有 STL 向量类型成员的类的复制内存
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- 是否可以有一个模板函数,可以将向量和非向量类型都作为参数
- 具有基元类型的模板向量类型
- 在索引和值类型上参数化的惯用向量类型
- 使用迭代器成员函数是否仅适用于某些向量类型"empty()"?
- 通过glUniform 将 GLM 的向量类型传递给 OpenGL
- 尝试将用户定义的向量类型插入用户定义的类型向量
- 使用类作为向量类型时出错 - "Undeclared identifier"
- 在C++中,当两个向量类型不同时,如何将值存储到向量内部的向量中
- 如何访问和存储向量类型的结构成员
- 按参数中的向量类型重载函数
- boost::variant:具有递归向量类型的奇怪行为
- LLVM 传递计数向量类型指令
- 用异构boost::融合向量类型数据填充std::vector
- 如何定义自定义类的向量类型
- 真正的动态向量类型
- 如何在类的属性/变量中使用组合类作为向量类型
- 更快地删除向量类型