Reputation: 129
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?
Upvotes: 1
Views: 12877
Reputation: 206607
Instead of
BankAccount& findAccount(std::string& accName)
use
BankAccount& findAccount(std::string const& accName)
When the function is called with "james"
, that cannot be converted to a std::string&
but it can be converted to a temporary std::string
, which can be used as an argument when the argument type is std::string const&
.
Also,
std::cout << db.findAccount(std::string("james"));
is a problem since there is no operator<<()
defined between std::ostream
and BankAccount
.
Upvotes: 2
Reputation: 951
You defined two overloads for findAccount, one taking an int and another one taking a std::string reference. The argument you tried to call it with is not a std::string, but a c-string, that is a const char*. The compiler now has two options:
In order to call the int version a conversion from const char* to int would be needed(this is the error you see).
In order to call the string version a temporary std::string would have to be created. After this, a non-const reference to a temporary would have to be formed(which is not allowed and the error you either dont see or did not show in your question).
Since you do not seem to change the string a simple fix of this problem would be to take a const std::string& instead. Your code has some other problems, too.
Upvotes: 1