Reputation: 1359
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:
Instead it prints this when I give it the test data:
"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?
Upvotes: 0
Views: 108
Reputation: 61960
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.
Upvotes: 5