0

First I declared my overloaded function similar to this question and had the same error as there. I changed as the answer suggest, but still got this error:

[Error] 'std::ostream& Number::operator<<(std::ostream&, const Number&)' must take exactly one argument

Code:

    //numb.hpp   
    class Number {   
        public:
        .....    
        std::ostream &operator<<(std::ostream&, const Number&);  
    };  

    //numb.cpp  
    std::ostream& Number::operator<<(std::ostream &output, const Number &Num){
                output << "The integers this object stores are: " << Num.num << " and " << Num.num1;
                return output; 
             }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
casper
  • 259
  • 4
  • 18
  • Overloading operators as member functions or non-member functions sometimes means different things. In your case you should not overload it as a member function. Please go through your books, tutorials or class-notes again. Or perhaps [get a couple of new good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jul 01 '19 at 10:34

1 Answers1

0

What you have there are two different declarations - first one declares a global overloaded operator, the second one declares (and defines) an operator that's a member of class Number (notice the Number::?). First one is OK (if that's what your intention is - to define it as a free standing operator), second one is obviously not. As explained is the answer you linked and as the error says - member operators need to take one argument (besides the implicit this argument).

jrok
  • 54,456
  • 9
  • 109
  • 141
  • i have edited the question, i forgot to mention that my overloaded function is within class Number, it is not global oveloaded operator – casper Jul 01 '19 at 10:52
  • Except that it will not work. What I assume you want is `cout << num;` where `num` is type `Number`. You *need* a free standing operator overload. – jrok Jul 01 '19 at 10:58