c++ - Getting an error of the form "operator<< must take exactly one argument" when overloading operator<< -


i have following code bigint class , i'm trying overload operator<<:

class bigint { private:     int numdigits;     char vals[];  public:             friend std::ostream& operator <<( std::ostream& os , const bigint &param ); };   std::ostream& bigint::operator <<( std::ostream& os , const bigint & param ) {     os <<" number of bits " <<param.numdigits << " , it`s   valeuris" <<param.vals<<"\ n ";     return os; }; 

i keep getting error:

xxxxx must take 1 argument.

i have searched lot on error. know should make operator<< friend function in class or declare out of class, , took care return of operator <<. strange either way i'm getting error.

can me?

i think issue here you're (probably unintentionally) mixing , matching 2 different approaches.

when write

friend std::ostream& operator <<( std::ostream& os , const bigint &param ); 

inside of class definition, saying there free function named operator << takes in ostream , bigint. when write

std::ostream& bigint::operator <<( std::ostream& os , const bigint & param ) {     ... }; 

you defining member function of bigint named operator << taking 2 arguments. 2 arguments, combined implicit this pointer, adds 3 arguments - more intended. note although function called operator<<, it's not operator<< declared friend in class definition.

to fix this, have few options. first, when define operator<<, can omit bigint:: prefix, fix things. alternatively, combine implementation friend definition inside class definition:

friend std::ostream& operator <<( std::ostream& os , const bigint & param ) {     ... } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -