Sign in to follow this  
OneThreeThreeSeven

What the heck...ostream errors o

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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';
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this