Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:37:34+00:00 2026-05-14T21:37:34+00:00

In a project I’m working on, I have a Score class, defined below in

  • 0

In a project I’m working on, I have a Score class, defined below in score.h. I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is printed.

Here’s what I tried to do:

ostream & Score::operator<< (ostream & os, Score right)
{
    os << right.getPoints() << " " << right.scoreGetName();
    return os;
}

Here are the errors returned:

score.h(30) : error C2804: binary 'operator <<' has too many parameters

(This error appears 4 times, actually)

I managed to get it working by declaring the overload as a friend function:

friend ostream & operator<< (ostream & os, Score right);

And removing the Score:: from the function declaration in score.cpp (effectively not declaring it as a member).

Why does this work, yet the former piece of code doesn’t?

Thanks for your time!

EDIT

I deleted all mentions to the overload on the header file… yet I get the following (and only) error. binary '<<' : no operator found which takes a right-hand operand of type 'Score' (or there is no acceptable conversion)
How come my test, in main(), can’t find the appropriate overload? (it’s not the includes, I checked)

Below is the full score.h

#ifndef SCORE_H_
#define SCORE_H_

#include <string>
#include <iostream>
#include <iostream>

using std::string;
using std::ostream;

class Score
{

public:
    Score(string name);
    Score();
    virtual ~Score();
    void addPoints(int n);
    string scoreGetName() const;
    int getPoints() const;
    void scoreSetName(string name);
    bool operator>(const Score right) const;

private:
    string _name;
    int _points;

};
#endif
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:37:35+00:00Added an answer on May 14, 2026 at 9:37 pm

    Note: You might want to look at the operator overloading FAQ.


    Binary operators can either be members of their left-hand argument’s class or free functions. (Some operators, like assignment, must be members.) Since the stream operators’ left-hand argument is a stream, stream operators either have to be members of the stream class or free functions. The canonical way to implement operator<< for any type is this:

    std::ostream& operator<<(std::ostream& os, const T& obj)
    {
       // stream obj's data into os
       return os;
    }
    

    Note that it is not a member function. Also note that it takes the object to stream per const reference. That’s because you don’t want to copy the object in order to stream it and you don’t want the streaming to alter it either.


    Sometimes you want to stream objects whose internals are not accessible through their class’ public interface, so the operator can’t get at them. Then you have two choices: Either put a public member into the class which does the streaming

    class T {
      public:
        void stream_to(std::ostream&) const {os << obj.data_;}
      private:
        int data_;
    };
    

    and call that from the operator:

    inline std::ostream& operator<<(std::ostream& os, const T& obj)
    {
       obj.stream_to(os);
       return os;
    }
    

    or make the operator a friend

    class T {
      public:
        friend std::ostream& operator<<(std::ostream&, const T&);
      private:
        int data_;
    };
    

    so that it can access the class’ private parts:

    inline std::ostream& operator<<(std::ostream& os, const T& obj)
    {
       os << obj.data_;
       return os;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.