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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:44:29+00:00 2026-06-15T20:44:29+00:00

I have an album class which is designed to store and Artist Name (string),

  • 0

I have an album class which is designed to store and Artist Name (string), albumtitle (string) and a list of track objects (vector). I’m trying to overload the “<<” operator to enable stream-based output.

The relevant code is this:

std::ostream& Album::printTracks (std::ostream &out, std::vector<Track> &t)
{
    unsigned int i;
    for (i=0; i<t.size(); i++)
        out << " " << t.at(i);
     return out;
}
std::ostream& operator<< (std::ostream &out, Album &a)
{
    out << "Artist name: " << a.artistName << "\n" <<
        "Album Title: " << a.albumTitle << "\n" <<
        "Tracks: " << a.printTracks(out,a.getTracks());
    return out;
}

Which should print, in this order:

  • The Artist’s name
  • The Album Title
  • The tracklist

Instead it prints this when I give it the test data:

  • The tracklist
  • The artist name
  • The album title

“Tracks:” followed by a memory location.

Constructor for "Track Class" is:
Track::Track (std::string t, Duration* d)
    {
        title = t;
        duration = d;
    }

The code overloading “<<” in the “track” class is:

std::ostream& operator<< (std::ostream &out, Track &t)
    {
    out << "Title: " << t.title << "\n" <<
        "Duration: " << *t.duration << "\n";
    return out;
    }

And eventual code being used for output is:

Duration* d = new Duration(3,4,50); //creating duration objects for testing
Duration* d2 = new Duration(5,7,300);
Duration* d4 = new Duration(3,3,50);
Track t1 = Track("Test",d); //creating track objects
Track t2 = Track("Test2",d2);
Track t3 = Track("Test3",d4);
std::vector<Track> tracks; //forming tracks into vector
tracks.push_back(t1);
tracks.push_back(t2);
tracks.push_back(t3);
Album a = Album("Test Artist","Test Album",tracks); //create album object
cout << a << endl; // output album object

Was wondering why the ordering doesn’t print as expected?

  • 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-15T20:44:31+00:00Added an answer on June 15, 2026 at 8:44 pm

    It’s unspecified what order your arguments will be evaluated in. One of them has side effects (printing the tracks), so if it’s evaluated first, you’ll see those printed first.

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

Sidebar

Related Questions

Suppose I have the following list: ID3_tag = ['Title','Artist','Album','Track'] And I have a file
newbie doing Java homework here. I have one class named Album which contains the
I have a collection called Albums with objects from the class Album . This
I have a image folder which contains sub directory for each album of images
I have the following models: class Song < ActiveRecord::Base belongs_to :albums end class Album
I'm trying to add entity to SQL CE 4 which have property of type
I have a method that returns an array of custom class objects that are
Suppose i have : class Album(models.Model): photos = models.ManyToManyField('myapp.Photo') photo_count = models.IntegerField(default = 0)
Ok, i got a error class, where you can do $error->add($message); which will store
I have User and Album models with HABTM relationship class Album < ActiveRecord::Base has_and_belongs_to_many

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.