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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:58:04+00:00 2026-05-27T02:58:04+00:00

I just started looking at structs in C++ and thought I might try and

  • 0

I just started looking at structs in C++ and thought I might try and figure out how to overload the stream insertion operator to take and object of Line (which itself contains objects of Point). I think I need some sort of declaration of overloading in Line? and possibly point? I found some similiar questions but to be honest I can’t figure it out at all.

It’s a very simple program so hopefully someone could take the time to look at it and explain to me how I should be going about it?

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::istream;

//define Point & Line type
struct Point{
    float x, y;
};
struct Line{
    Point p1, p2;
    istream& operator>>( istream& in, const Line& line); //something like this here?
};
//function declarations
Point calcMidpoint(const Line& rline);

//operator overload
istream& operator>>( istream& in, const Line& line){
    in >> line.p1.x >> line.p1.y >> line.p2.x >> line.p2.y;
    return in;
}

//MAIN
int main(){
    Line line;
    cout << "please enter one pair of x and y values followed by another like so (x1 y1 x2 y2): ";
    cin >> line;
    //get midpoint of line
    Point mp;
    mp = returnMidpoint(line);
    cout << "The Midpoint is.. (" << mp.x << " " << mp.y << ")" <<endl;

    return 0;
}

//can be used in a large expression at the expence of creating temp instances
Point calcMidpoint(const Line& rline){
    Point midpoint;
    midpoint.x = (rline.p2.x + rline.p1.x) / 2;
    midpoint.y = (rline.p2.y + rline.p1.y) / 2;
    return midpoint;
}
  • 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-27T02:58:05+00:00Added an answer on May 27, 2026 at 2:58 am

    Binary operators can only be defined as member functions if the first operand is of the type of the class. Since this isn’t the case (the first operand is std::istream&), you must define a free function:

    class Foo;
    
    std::istream & operator>>(std::istream & is, Foo & x)
    {
      //...
      return is;
    }
    

    It might be useful to declare this function a friend inside your class so it can access private members.

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

Sidebar

Related Questions

I am just started out learning Python and also started looking into Django a
I'm started looking at Subsonic just yesterday and having trouble figuring out how to
How would you test this scenario? I've just started looking into NHibernate and having
Just some days ago I started looking into a unit test framework called check,
I've just started at a new communications company, and we're looking at a workflow
I have been looking at using TDD and implementing proper testing (only just started
I'm looking for a fuzzy date algorithm. I just started writing one and realised
HI all, I'm just getting started with developing for Android. I'm looking to port
Just started looking into encryption using keys and certificates in sql server 2005/08 and
I Just started looking at this technology , but i am finding it hard

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.