Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Outputting the objects out of a vector<object*> c++

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading