|
-
May 4th, 2002, 11:12 AM
#1
why can't I have any good friends?
My friend statments seem to be written wrong or something. Could someone look at these and see if they are written correctly?
Code:
friend ostream & operator << (ostream & o, const money m);
friend istream & operator >> (istream i, money m);
Thanks!
NOMAD
-
May 5th, 2002, 11:42 AM
#2
Leave the return values as references, the other things like riis said.
What errors do you get? Where did you put those declarations?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 5th, 2002, 01:32 PM
#3
riis, thanks for the syntax. I should get a book that does a better job with classes. Still get the same errors.
cornedbee,
Code:
c:\windows\desktop\cs\cs131\assignment 4\money.h(25) : error C2143: syntax error : missing ';' before '<<'
c:\windows\desktop\cs\cs131\assignment 4\money.h(25) : error C2433: 'ostream' : 'friend' not permitted on data declarations
c:\windows\desktop\cs\cs131\assignment 4\money.h(25) : error C2501: 'ostream' : missing storage-class or type specifiers
c:\windows\desktop\cs\cs131\assignment 4\money.h(25) : error C2244: 'ostream' : unable to resolve function overload
c:\windows\desktop\cs\cs131\assignment 4\money.h(25) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cs\cs131\assignment 4\money.h(25) : error C2059: syntax error : '&'
c:\windows\desktop\cs\cs131\assignment 4\money.h(26) : error C2143: syntax error : missing ';' before '>>'
c:\windows\desktop\cs\cs131\assignment 4\money.h(26) : error C2433: 'istream' : 'friend' not permitted on data declarations
c:\windows\desktop\cs\cs131\assignment 4\money.h(26) : error C2501: 'istream' : missing storage-class or type specifiers
c:\windows\desktop\cs\cs131\assignment 4\money.h(26) : error C2244: 'istream' : unable to resolve function overload
c:\windows\desktop\cs\cs131\assignment 4\money.h(26) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cs\cs131\assignment 4\money.h(26) : error C2059: syntax error : '&'
Here is the class declaration:
Code:
#ifndef MONEY_H
#define MONEY_H
#include <iostream>
class money
{
protected:
long int cents;
public:
money ();
money (const money & m);
money operator = (money m);
long int getCents ();
void setCents (long int l);
bool operator < (money m);
bool operator == (money m);
void operator = (int i);
money operator + (money m);
money operator - (money m);
money operator * (float f);
money operator * (int i);
friend ostream operator<< (const ostream &o, const money &m);
friend istream operator>> (const istream &i, const money &m);
//friend ostream & operator << (ostream & o, const money m);
//friend istream & operator >> (istream i, money m);
};
#endif
And here are the friends in the cpp file
Code:
friend ostream operator<< (const ostream &o, const money &m);
//ostream & operator << (ostream & o, money m)
{
o << (m.cents / 100) << '.';
long int x = (m.cents % 100);
if (x < 10)
o << char (48);
o << x;
return o;
}
friend istream operator>> (const istream &i, const money &m);
//istream & operator >> (istream i, money m)
{
float c;
i >> c;
m.cents = ((long int) ((c + 0.005) * 100.0));
return i;
}
-
May 5th, 2002, 01:43 PM
#4
Monday Morning Lunatic
Try using friend std: stream& ..., etc.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 5th, 2002, 01:56 PM
#5
parksie, THANK YOU!
I can't believe I didn't have the using statements after iostream. I think I maybe relying on the forum too much. Anyway, thanks all of you for your help!
-
May 5th, 2002, 06:45 PM
#6
Fanatic Member
well as long as ur learning and not just getting HW answers
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|