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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:27:01+00:00 2026-06-01T04:27:01+00:00

This is my test program. The variable s1 contains a vector that contains objects

  • 0

This is my test program. The variable s1 contains a vector that contains objects of a user-defined type. I want to overload the << operator to print this vector nicely, one element per line.

#include <iostream>
#include "myclasses.h"
using namespace std;

int main() {

    myclass s1;

    s1.add_point(7000, 10);
    s1.add_point(8000, 11);
    s1.add_point(9000, 12);

    cout << s1 << endl;

    return 0;

}

Here’s the class definition. Basically it’s just an array of another user-defined objects. I want the << operator to print all entries, one per line. Assume that “anotherclass” has the needed support methods.

class myclass {
    vector<anotherclass> ps;
public:
    void add_point(double, double);
    friend ostream &operator<<( ostream &out, const myclass &s );
};

void myclass::add_point(double first, double second) {
    ps.push_back(anotherclass(first, second));
}

ostream &operator<<( ostream &out, const myclass &s ) {
    vector<anotherclass>::iterator itr;
    vector<anotherclass> psp=s.ps; // Why do I need this? Why can't I use s.ps.begin() and s.ps.end() in the for loop directly?
    for ( itr=psp.begin() ; itr != psp.end() ; ++itr ) {
        cout << "(" << itr->get_first() << "," << itr->get_second() << endl;
    }
    return out;
}

I have commented one line that I needed to add to get this to work in my program. I have no idea why that line is needed. Could somebody explain it to me?

  • 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-01T04:27:03+00:00Added an answer on June 1, 2026 at 4:27 am

    The iterator member type always allows you to modify the referenced object. Therefore you can’t get an iterator from a const & argument such as s. You need to declare the local variable to have type vector< anotherclass >::const_iterator. Every container class has a const_iterator in parallel with iterator.

    The reason your one-line fix works is that the newly-constructed vector is not const, unlike the function parameter it was constructed from.

    In C++11, you can just use auto itr = s.ps.begin() and not ever name the iterator type. Or just use for ( auto const &obj : s.ps ) and forgo iterators completely.

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

Sidebar

Related Questions

I wrote a test program that looked like this: #!/usr/bin/python def incrementc(): c =
Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {
I'm running this small C# test program launched from a pre-commit batch file private
Lets say I have hierarchy like this (This is just a test program. Please
I need to run this line from my c++ program: java -jar test.jar text1
Having a program like this: #include <iostream> #include <string> using namespace std; class test
I have a text file named test.txt I want to write a C program
I am working on a java program that is essentially a chat room. This
I was wondering how I could prompt the end-user of my program to type
I have a test program that I wrote to try and debug a GMutex

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.