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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:26:55+00:00 2026-06-14T14:26:55+00:00

I was wondering if anyone would be able to help me out with regards

  • 0

I was wondering if anyone would be able to help me out with regards to printing out a list of a custom class (first time using containers).

Currently I have a variable declared as:

std::list<Item> inventory;

within a class called player.

Now I have created a function within the class (player) called void printInventory();.

So my question is how do I go about printing what is in that list.

My Item class contains 3 variables;

std::string name; 
int damage;
int value;

I also have a function to print these variables void itemDetails();

Any help is much appreciated.

Edit:

I know have solved my problem with thanks to the answers provided, here’s what i did:

Overloaded the output operator in the item class:

    ostream& operator<<(ostream& os, const Item& item)
    {
            if (item.getTypeInt() == 0)
            {
                    os << "Name: " << item.getName() << endl
                    << "Type: " << item.getTypeString() << endl
                    << "Damage: " << item.getDamage()<< endl
                    << "Value: " << item.getValue() << endl;
            }
            else
            {
            os << "Name: " << item.getName() << endl
            << "Type: " << item.getTypeString() << endl
            << "HP: " << item.getHP()<< endl
            << "Value: " << item.getValue() << endl;
    }

    return os;
    }

I then used one of the answers but modified it so it wasn’t declaring another variable:

    void Player::printInventory()
    {   
        for(std::list<Item>::iterator it = inventory.begin(); it!= inventory.end(); ++it)
        {
                cout << *it;
        }

        cout <<"Inventroy Printed!!"<<endl;
    }
  • 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-14T14:26:56+00:00Added an answer on June 14, 2026 at 2:26 pm

    You should overload std::ostream& operator<< for Item and player, this will allow you to write the data to streams other than stdout. Here’s an example:

    std::ostream& ooprator<<(std::ostream& os, const Item& item)
    {
      return os << "Item {" << item.name << " " << item.damage << " " << item.value << "}";
    }
    

    This allows you to do

    Item i;
    std::cout << i << "\n";
    

    Then, you implement a similar operator for player, looping over the list and printing each item.

    std::ostream& ooprator<<(std::ostream& os, const player& p)
      for (const auto& i : p.inventory) os << i << " "
      return os;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anyone would be able to help me convert the below
I was wondering if anyone would be able to help me fix this script...It
Just wondering if anyone would be able to help with the following VBScript script
I was wondering if anyone would help with the following: I have a date
I'm wondering if anyone can help me out with some Canvas. I'm pretty new
I am wondering if anyone out there on here has been able to succesfuly
http://www.liveprofile.com/ I was wondering if anyone would be able to explain roughly how an
Hi brothers and sisters, I am wondering if anyone would share their experience on
I am wondering if anyone knows a java script library that would add a
I'm building an application using ASP.net MVC 3 and I'm wondering if anyone knows

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.