I have two classes:
class Transactions
{
public:
std::string type= "";
double value = 0;
void toString();
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
class account
{
private:
double balance = 0;
std::vector <Transactions*> history{};
public:
double get_balance() { return balance; }
void set_balance(double value) { balance = value; }
void toString();
std::vector <Transactions*> get_history() { return history; }
void set_history(Transactions* His) { history.push_back(His); }
};
I am in the process implementing the toString() method by doing:
void account::toString() {
std::cout <<" | Current Account | Balance:" << std::to_string(get_balance()) << " | " << std::endl
<< "Transaction History: ";
for (Transactions* transaction : get_history())
{
std::cout << transaction->toString();
}
}
but I get an error on the "<<" in the for loop. I also tried using an ordinary for loop but came to the same conclusions of "C++ no operator matches these operands. operand types are: std::ostream << void". I am fairly new to c++ coming from a c#/python background where pointers aren’t a thing so I can only assume that is where the issue is being raised? That or that the vector is empty, but when the program is running it will be filled so I’m not sure how to circumvent that issue
also this is where account and transaction objects are made:
account* curAccount = new account(std::stoi(parameters[2]), accID++);
accountStore.push_back(curAccount);
std::cout << "account open! The account number is " << accID;
Transactions* openingCTrans = new Transactions("Opening_Balance", std::stoi(parameters[2]));
curAccount->set_history(openingCTrans);
I know I didn’t show the account or transaction constructors in the class but I don’t think they’re the issue
>Solution :
this:
class Transactions
{
public:
std::string type= "";
double value = 0;
void toString();
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
should be
class Transactions
{
public:
std::string type= "";
double value = 0;
std::string toString(); <<<<<============
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
because you are doing
std::cout << transaction->toString();
ie, "please print the output of this function" so it has to return something printable
and use std::shared_ptr – you will thank me