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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:05:38+00:00 2026-06-14T19:05:38+00:00

I have a class MyList that overrides the << operator to be able to

  • 0

I have a class MyList that overrides the << operator to be able to log itself to the console:

class MyList {
public:
    vector<int> *numbers;
};
ostream& operator<<(ostream& os, MyList& l);

Implementation:

ostream& operator<<(ostream& os, MyList& l) {
    for (int i = 0; i < l.numbers->size(); i++) {
        os << l.numbers->at(i);         
    }
    return os;
}

In an other class I have a member variable of type MyList and I can’t print it to the console. Interestingly a local MyList variable works fine:

class A {
public:
    MyList list;
    void someMethod() const;
};

Implementation:

void A::someMethod() const {
    MyList local;
    // Set up local list.
    cout << "Local:" << local; // OK!
    cout << "Member:" << list; // ERROR!
}

This is the error message:

Invalid operands to binary expression ('basic_ostream<char,
std::__1::char_traits<char>>' and 'const MyList')

Xcode’s auto-fix recommends to reference list:

cout << "Member:" << &list;

This will compile, but (obviously) it prints the adress of list and not my content. I don’t understand what the difference is between those two variables in regards to the << operator. Could anybody explain?

  • 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-14T19:05:38+00:00Added an answer on June 14, 2026 at 7:05 pm

    I don’t think this is your real code, but here’s my guess:

    ostream& operator<<(ostream& os, const MyList& l)
    //                                 |
    //                             note const
    

    Either that, or something really dumb like forgetting a trailing ;:

    class A {
    public:
        MyList list;
        void someMethod();
    };  // <------- here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For instance, if I have an class: public class StuffHolder { List<Stuff> myList; public
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have a: public class MyList extends ListActivity { SimpleAdapter simpleAdapter; Binding to the
I have a class similar to the following that uses an internal List: public
let's have this code : class MyList : IEnumerable, IEnumerator { int[] A =
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have class with back reference: public class Employee : Entity { private string
I have: One MyListActivity class extends ListActivity setContentView(R.layout.mylist) ; Two xml files: mylist.xml and
I have a class with a list property that seems to lose an element

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.