Results 1 to 6 of 6

Thread: why can't I have any good friends?

  1. #1
    NOMADMAN
    Guest

    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    NOMADMAN
    Guest
    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;
    }

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    NOMADMAN
    Guest
    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!

  6. #6
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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
  •  



Click Here to Expand Forum to Full Width