Thread: Newbie help please

  1. #1
    Registered User
    Join Date
    Dec 2006

    Newbie help please

    Hello everyone, I am new here and I was wondering if anyone could please give me some help!...I have a program that is just driving me nuts and I was wondering if anyone could help me out? is the when I run this program I get 52 errors but I just want to start with the first one and then work on it and see if I can contiune!...the first error is that it says: error C2804: binary 'operator <' has too many parameters
    error C2333: 'CNum:perator`<'' : error in function declaration; skipping function...can anyone please explain to me why?...Thank you very much in advance and sorry to trouble you all if this is really simple:

    #ifndef __CNUM_H__
    #define __CNUM_H__
    #include <iostream>
    class CNum
    		float R;
    		float I;
      		friend std::ostream& operator <(std::ostream & out, CNum& X);
       		friend std::istream& operator >(std::istream & in, CNum& X);
    		CNum operator+(const CNum& B) const; 
    		CNum& operator++();    			
    std::ostream& operator <(std::ostream & out, CNum& X)
    	std::out<< "("<< X.R <<"," << X.I<<")"<<"\n";
     	return out;
    std::istream& operator >(std::istream & in, CNum& X)
    	std::in >> X.R >> X.I;
          return in;

  2. #2
    Registered User
    Join Date
    Dec 2006
    try putting the operator functions outside of the class declaration, since they are not part of the class

    edit: there are two ways of doing operator overloads. The first is as an instance method of a class, then the first argument of the operator is the instance itself, and the other operands are parameters so e.g. for < it would only need one other operand as a parameter. The second is as a global function not part of a class, then it would require all the operands as parameters. This is an example of the second type, since the first operand is istream / ostream, and we are not messing with those classes; so we put them as global functions.
    Last edited by spoon!; 12-15-2006 at 04:24 AM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Thanks spoon for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM