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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:05:04+00:00 2026-06-05T21:05:04+00:00

ostream& operator<<(ostream& os, const PT& p) { os << ( << p.x << ,

  • 0
ostream& operator<<(ostream& os, const PT& p)
{
   os << "(" << p.x << "," << p.y << ")";
}

PT is a structure and x , y are its members.
Can someone please explain what exactly the above line does. Can’t the desired text be printed using cout?

I came across this snippet of code from this site.

  • 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-06-05T21:05:06+00:00Added an answer on June 5, 2026 at 9:05 pm

    This provides a method of outputting the PT. Now, you can use this:

    PT p;
    std::cout << p;
    

    This gets translated into a call of

    operator<< (std::cout, p);
    

    That matches your overload, so it works, printing the x and y values in brackets with less effort on the user’s part. In fact, it doesn’t have to be cout. It can be anything that “is” a std::ostream. There are quite a few things that inherit from it, meaning they are std::ostreams as well, and so this works with them too. std::ofstream, for file I/O, is one example.

    One thing that the sample you found doesn’t do, but should, though, is return os;. Without doing that, you can’t say std::cout << p << '\n'; because the result of printing p will not return cout for you to use to print the newline.

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

Sidebar

Related Questions

The error occurs when I try to do this friend std::ostream& operator<<(std::ostream& os, const
I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful.
template<class U, class W> std::ostream& operator<<( std::ostream & out, const Alias<U, W> & A)
why does void operator<<(ostream out, Test &t); return an error whereas void operator<<(ostream &out,
In the following code: using namespace std; //ostream& operator<< (ostream& out,const string & str)
UPDATE: The following code gives me an error Graph.cpp: In function 'std::ostream& operator<<(std::ostream&, const
#include <iostream> using namespace std; class Array { friend ostream &operator<<( ostream &, const
ostream& operator <<(ostream& osObject, const storageRentals& rentals) { osObject << rentals.summaryReport(); return osObject; }
This function declaration gives me errors: ostream& operator<<(ostream& os, hand& obj); The errors are:
I think the following code can be used to create manipulators. #include<iostream> ostream &

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.