I’m relatively new to C++, and having an issue passing a string. I have 2 constructors for a class, Transaction.” One constructor accepts a string and a double as its parameters, while the other accepts just a string.
When I attempt to pass the lines below, I get an error, saying:
no matching function for call to 'Account::addTransaction(const char [14])'
or
no matching function for call to 'Account::addTransaction(const char [11], double&)'
I know that there’s no matching function, because I’m passing a string! Here’s what I’m passing in:
bank.getAccount(index).addTransaction("Close Account");
bank.getAccount(index).addTransaction("Withdrawal", amount_to_withdraw);
I don’t know how to make it any more explicit that the first parameter is a string. Any advice would be greatly appreciated.
Thanks,
Adam
Updating per @g24l ‘s request:
Here is the Transaction Class:
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <iostream>
#include <string>
using namespace std;
class Transaction {
private:
string transType;
double transAmount;
/*
public constructors:
* 1st constructor is the default constructor
* 2nd constructor is for non-monetary transactions
* 3rd constructor is for transactions involving money
*/
public:
Transaction() {
transType = "";
}
Transaction(string tType) {
transType = tType;
}
Transaction(string tType, double tAmount) {
transType = tType;
transAmount = tAmount;
}
void setTransType(string);
void setTransAmount(double);
string getTransType() const;
double getTransAmount() const;
};
#endif /* TRANSACTION_H */
In the Account class, which uses dynamic memory allocation for an array of transactions, I have:
class Account{
private:
Depositor depositor;
int accountNum;
string accountType;
double accountBalance;
string accountStatus;
Transaction *transptr;
int numTransactions; //number of transactions
public:
// public member functions prototypes
// Constructors
/* Account default constructor:
* Input:
* Depositor() - calls the default depositor constructor
* Process:
* sets object's data members to default values
* Output:
* object's data members are set
*/
Account()
{
//cout << "Account default constructor is running" << endl;
Depositor();
accountNum = 0;
accountType = "";
accountBalance = 0.0;
accountStatus = "open";
transptr = new Transaction[100];
numTransactions = 0;
}
I’m wondering if, when I declare the array of Transactions, it fills in all of the Transactions with the default constructor parameters. When I “add” a transaction I’m really writing over the existing transaction.
bank.getAccount.addTransactiontakes aTransactionas a parameter. So pass it one: