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 6619193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:56:07+00:00 2026-05-25T20:56:07+00:00

Trying to study C++ function templates. As a part of which I have this

  • 0

Trying to study C++ function templates. As a part of which I have this code below. It works fine, but I have questions as below :-

1] Why is the operator << overloading function need to be friend ? If I remove the keyword friend it gives compilation error saying : operator << has too many parameters.

2] Why does the operator << overloading function need to return a reference to ostream object which is also an input argument to it?

3]I doubt this, but Do the above 2 questions have got anything to do with the fact that the function templates are used for a user defined class which has overloaded functions?

template <class T>
T Average(T *atArray, int nNumValues)
{
    T tSum = 0;
    for (int nCount=0; nCount < nNumValues; nCount++)
        tSum += atArray[nCount];

    tSum /= nNumValues;
    return tSum;
}

class Cents
{
private:
    int m_nCents;
public:
    Cents(int nCents)
        : m_nCents(nCents)
    {
    }

    //Why is friend needed below

    //Why does it need to return ostream&, why can't it have void return type, as all it is doing is printing the class private member.
    friend ostream& operator<< (ostream &out, const Cents &cCents)
    {
        out << cCents.m_nCents << " cents ";
        return out;
    }

    /* 
    void operator <<( const Cents &cCents) //did not work - compilation errors
    {
        cout << cCents.m_nCents << " cents ";
    }
    */

    void operator+=(Cents cCents)
    {
        m_nCents += cCents.m_nCents;
    }

    void operator/=(int nValue)
    {
        m_nCents /= nValue;
    }
};

int main()
{
    int anArray[] = { 5, 3, 2, 1, 4 };
    cout << Average(anArray, 5) << endl;

    double dnArray[] = { 3.12, 3.45, 9.23, 6.34 };
    cout << Average(dnArray, 4) << endl;

    Cents cArray[] = { Cents(5), Cents(10), Cents(15), Cents(14) };
    cout << Average(cArray, 4) << endl;

    cin.get();
    return 0;
}
  • 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-25T20:56:08+00:00Added an answer on May 25, 2026 at 8:56 pm

    Why is the operator << overloading function need to be friend ? If I remove the keyword friend it gives compilation error saying : operator << has too many parameters.

    << alters the stream’s state, and hence ideally it should be implemented as member of it’s left operand’s type. However, its left operands are streams from the standard library, and while most of the stream output and input operators defined by the standard library are indeed defined as members of the stream classes, when you implement output and input operations for your own types, you cannot change the standard library’s stream types.
    That is why you need to implement these(<< and >>) operators for your own types as non-member functions. Since you need to access the private/protected member variables of your class object inside the operator definition, these overloaded operators are needed to be declared as friend of your class.

    Why does the operator << overloading function need to return a reference to ostream object which is also an input argument to it?

    Returning an reference to standard stream object allows you to have Object Chaining.

    You can have calls like:

    out<<obj1<<obj2;
    

    Templates are used for a user defined class which has overloaded functions?

    Templates help you implement generic functions and classes which can be called for different
    datatypes and the compiler takes care of generating the code for those specific data types. So the above two points are not related.


    Strongly suggest reading this FAQ entry:
    Operator overloading

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to study OpenGL and I have the framework added, but I
I am trying to study the jquery class, but I have a hard time
I'm trying to code an md5 hashing function in Python but it doesn't seem
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
At first I am not familar with HTML. I have been trying to study
I'm trying to implement this extenstion of the Karplus-Strong plucked string algorithm, but I
I'm trying to implement Dijkstra's algorithm in Java (self-study). I use the pseudo-code provided
I don't have a problem with ReSharper but some where in my study I
I'm trying to studdy some performace things... this question may sound stupid, but I'll
Ok, i'm a newbie in this, i've been trying to study MVC patterns and

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.