Overloaded operator problem
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;
}
}
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;
}
}
this should work.
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;
}
}
this should work.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement