I have four files in c++
BankAccount.h
BankDatabase.h
Main.cpp
BankAccount.h
#include <string>
class BankAccount
{
public:
BankAccount(int accNumber,const std::string& accName);
void setAccNumber(const int accNumber);
int getAccNumber() const;
void setAccName(const std::string& clientName);
std::string getAccName() const ;
protected:
int mAccNumber;
std::string mAccName;
};
BankAccount::BankAccount(const int accNumber,
const std::string& accName):mAccNumber(accNumber),mAccName(accName){}
void BankAccount::setAccNumber(const int accNumber)
{
mAccNumber = accNumber;
}
void BankAccount::setAccName(const std::string& accName)
{
mAccName = accName;
}
int BankAccount::getAccNumber() const
{
return mAccNumber;
}
std::string BankAccount::getAccName() const {
return mAccName;
}
BankDatabase.h
#include <map>
#include <iostream>
#include <stdexcept>
#include "BankAccount.h"
class BankDatabase
{
public:
BankDatabase();
void addAccount(const BankAccount& acc);
void deleteAccount(int accNumber);
BankAccount& findAccount(int accNumber)
throw (std::out_of_range);
BankAccount& findAccount(std::string& accName)
throw (std::out_of_range);
void mergeDatabase(BankDatabase& db);
protected:
std::map<int,BankAccount> mAccounts;
};
BankDatabase::BankDatabase(){}
void BankDatabase::addAccount(const BankAccount& acc)
{
std::pair<std::map<int,BankAccount>::iterator,bool> res =
mAccounts.insert(std::make_pair(acc.getAccNumber(),acc));
if(!res.second)
std::cout << "Account cannot be added ";
}
void BankDatabase::deleteAccount(int accNumber)
{
if( mAccounts.count(accNumber))
mAccounts.erase(accNumber);
}
BankAccount& BankDatabase::findAccount(int accNumber)
throw(std::out_of_range)
{
auto iter = mAccounts.find(accNumber);
if(iter != mAccounts.end())
return iter->second;
throw std::out_of_range("No account number with this name ");
}
BankAccount& BankDatabase::findAccount(std::string& accName)
throw(std::out_of_range)
{
for(auto& p : mAccounts) {
if ( p.second.getAccName() == accName )
return p.second;
}
throw std::out_of_range(" No account with this name ");
}
Main.cpp
#include <iostream>
#include "BankDatabase.h"
int main()
{
BankDatabase db;
db.addAccount(BankAccount(1,"james"));
db.addAccount(BankAccount(2,"johnson"));
db.addAccount(BankAccount(3,"kamal"));
db.addAccount(BankAccount(4,"appu"));
// find account name based on account number
std::cout << db.findAccount(1).getAccName();
// find account number based on account name
std::cout << db.findAccount("james");
// delete an account
db.deleteAccount(db.findAccount("james").getAccNumber());
// find the account
std::cout << db.findAccount("james");
//Merge database
db.mergeDatabase(db);
return 0;
}
At the statement std::cout << db.findAccount("james");
I got an error:
error: invalid conversion from 'const char*' to 'int'
But I defined findAccount(int)
and findAccount(string)
in BankDatabase.h
Can someone help in this issue?
Aucun commentaire:
Enregistrer un commentaire