Reputation: 928
I wanted to create a function that retrieves all the information from previous functions within the same Class, and prints the values that were returned in the format of a bunch of cout
statements, there is nothing for me to return in this PrintStatement() function, so I would create a void function, correct? My issue is in the int main()
, I cannot cout
a void function.
this is my account header file, and the function piece from my account.cpp file.
class Account {
public:
//Object constructor
Account(char firstName[], char lastName[], char sinNumber[], double balance, int accountType, int transactions);
//Object operations
double DepositAmt(double amount);
double WithdrawAmt(double amount);
void PrintStatement();
double getFinalBalance(double fbal);
string getAccountType();
double getTransactions (double Deposit, double Withdraw);
private:
//Object properties
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance;
int accountType;
int transactions;
};
void Account::PrintStatement()
{
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
cout << "SIN Number: " << sinNumber << endl;
cout << "Account Type: " << accountType << endl;
cout << "Final Balance: " << balance << endl;
cout << "Transactions: " << transactions << endl;
};
the global variables have already been initialized.
What I've tried:
I originally tried to cout << account.PrintStatement() << endl;
however I get an error C2679 (binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
I thought maybe changing things to apply to a string function instead would work, but instead I get a bunch of int conversion errors etc.
I'm unsure of what to do.
I am required to put these in a function just to be clear.
I tried using this question https://stackoverflow.com/questions/12766858/how-to-call-void-function-from-main to help me, it made sense that the poster was using a reference, but I do not have that. Is there another way?
Upvotes: 0
Views: 2092
Reputation: 209
Just call the method on your instance of the class. It doesn't need to return anything; it will do the counts you want and then return to main.
Upvotes: 4
Reputation: 911
When we use cout<<something_here
, the compiler will interpret it as "print the value of something_here
". Now, something_here
can be a lot of things. When it is a function, cout
will print the value returned by the function. In your case, the return type is void
i.e. nothing. So, there is nothing to print.
To fix your issue, you can directly call account.PrintStatement();
since you have already printed what you wanted to print inside this function.
Upvotes: 2
Reputation: 385295
I originally tried to
cout << account.PrintStatement() << endl;
Well, the expression account.PrintStatement()
is abject nothingness because the function has a void
return type. As you've indicated, the function returns nothing, so there is nothing to stream to cout
.
The function itself has already streamed a bunch of stuff to cout
, fulfilling all your cout
y needs. So, simply invoke it:
account.PrintStatement();
That's it!
Upvotes: 4