Sign in to follow this  
MasterDario

Overloaded operator problem

Recommended Posts

MasterDario    120
Hey all, in my class i overloaded the >> operator so that i can use cin on an object of a class. ex: Money MyMoney cin >> MyMoney I've got this working fine in int main(), but for some reason when i try to to put that into a function, it doesnt work. Its returning error: C2676 Money does not define this operator or a convertion acceptable to the predefined operator. Heres my function. Im thinking that maybe i'm passing in the function parameters incorrectly. // Reads in Money from Input File and stores in array of Money Objects void ReadInMoney(istream& in,Money& Arr,int Counter) { for(int i=0; i < Counter; ++i) { in >> Arr[i]; } }

Share this post


Link to post
Share on other sites
dragongame    538
Hi,

in the code you poste Arr is a reference and not an arry, but you use it like one. So the problem seams not to be your overloading, but your use of references and arrays.

void ReadInMoney(istream& in, Money* Arr, int Counter)
{
for(int i=0; i < Counter; ++i)
{
in >> Arr[i];
}
}

this should work.

Share this post


Link to post
Share on other sites
MasterDario    120
Actually, I just fixed the problem. I forgot that when passing in an array that you have to put the name followed by [].

One thing I find interesting that i completely forgot about is that arrays are automatically passed by reference. Since i completely forgot that, it was causing me some problems, but everything seems to be running okay now.

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