Public Group

# What the heck...ostream errors o

This topic is 3199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

//bill.h
#pragma once

class bill
{
private:
int amount;
public:
bill(void);
~bill(void);
void SetBill(int);
void ShowAmount() const;
bill & operator+(bill &);
};


//bill.cpp
#include "bill.h"
#include <iostream>

using namespace std;

bill::bill(void)
{
amount = 0;
}

bill::~bill(void)
{
}

void bill::SetBill(int x)
{
amount = x;

}

void bill::ShowAmount() const
{
cout << this->amount;
}

bill & bill::operator+(bill & x)
{
x.amount = this->amount + x.amount;

return x;
}


//mainprog.cpp
#include <iostream>
#include "bill.h"

using namespace std;

int main()
{
int number;
bill x;
bill y;
bill z;

cout << "Enter a number: ";
cin >> number;
x.SetBill(number);
y.SetBill(number);

z = x + y;

cout << z.ShowAmount();

return 0;

}


In this program, each class object is supposed to represent a monetary bill. I want to use operator overloading to add each class object and get the total amount of the bills. Anyways...I was just testing to see if my operator overloading worked...but I got all these wierd errors talking about ostream when I tried to compile it. I don't see what's wrong though. -edit- This is the main error here mainprog.cpp - error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion) -edit- ok nevermind. I see what I did. I called cout in the main program and in the class function definition.

##### Share on other sites
bill::ShowAmount() returns void, which is why the compiler complains. It tries to invoke the << operator with (std::ostream&, void), for which there is no reasonable overload. Either make bill::ShowAmount() return "amount" (in which case it should rather be called GetAmount) or change main() so that it does:

z.ShowAmount();

As opposed to:

cout << z.ShowAmount();

Remembering that ShowAmount() takes care of the printing. :)

##### Share on other sites
Rather than:

cout << z.ShowAmount(); // ShowAmount() returns a void type

which is cout << void;

try just

z.ShowAmount();

##### Share on other sites
thx for the responses. ;)

##### Share on other sites
Firstly you should post the actual errors your compiler gives you, not only does this make it easier for everyone else it also gives you an understanding of the compiler errors.

Normal convention is to capitalise the first letter of a class type.
Addition operator is unconventional it is actually preforming a += on the right hand side argument.
Void parameters are not required and are normally used from someone who is an old C user.
Constructors should really use initialisation lists for members where it is possible.
You should really provide a conversion constructor so you can create a bill from an amount, which would allow the following

bill bill::operator+(bill const & x)	return bill(amount+ x.amount);}

Now to the stream error. The following line is what is causing it
cout << z.ShowAmount();

The method ShowAmount does not return anything yet sends output to standard out. You either need to remove the start of the line "cout <<",pass the stream into the method to use, provide an stream operator for the class, provide a "getter" for the private member amount etc.

Edit:far too late, I got called away whilst writing the response :(

##### Share on other sites
hey when I create my object from the conversion constructor what do I do with it? I keep getting compile errors saying that I cannot access private members if I assign it to something in main().

##### Share on other sites
Quote:
 bill & bill::operator+(bill & x){ x.amount = this->amount + x.amount; return x;}

No!! And here's why:

class IntWrapper {private:	int _value;public:	IntWrapper() : _value( 0 ) { }	IntWrapper( int v ) : _value( v ) {	}	// Offending code:	IntWrapper& operator + ( IntWrapper & x ) {		x._value = this->_value + x._value;		return x;	}	// Use this code instead:	/*	IntWrapper operator + ( const IntWrapper & x ) const {		return IntWrapper( this->_value + x._value );	}	*/	// Or make it a operator+= instead	IntWrapper& operator += ( const IntWrapper & x ) {		this->_value += x._value;		return *this;	}};int main() {	int x = 2;	int y = 3;	int z = x + y; // This sets "z" to 5, as expected.	IntWrapper a = 2;	IntWrapper b = 3;	IntWrapper c = a + b; // This sets "c" to 5, but also sets "b" to 5! 	// double-you-tee-eff!}

Also the proper way to overload the insertion operator:
class Bill {	/* ... */};std::ostream& operator << ( std::ostream& outStream, const Bill& theBill ) {	outStream << theBill.getAmount();	return outStream;}int main() {	Bill myBill( 123 );	std::cout << "The bill is:  " << myBill << '\n';}

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632664
• Total Posts
3007708
• ### Who's Online (See full list)

There are no registered users currently online

×