I didn't find out how to.
My idea is create a track object and then gettracks. then iterate, but doesn't allow me, because tracks doesn't have the getTracks()
Code:
auto tracks = MyCds.GetTracks;
// for (unsigned i = 0; i < tracks.size(); ++i) {
// std::cout << tracks.at(i).GetTrackName() << std::endl;
// }
Update Code:
Code:
#include <iostream>
#include <vector>
#include <string>
class XTrackInfo
{
std::string m_TrackName;
int m_Length;
public:
XTrackInfo() {}
XTrackInfo(std::string TrackName, int Length):
m_TrackName(std::move(TrackName)),
m_Length(Length)
{}
void SetTrackName(std::string TrackName) { m_TrackName = std::move(TrackName); }
void SetTrackLength(int Length) { m_Length = Length; }
const std::string& GetTrackName() const { return m_TrackName; }
int GetTrackLength() const { return m_Length; }
};
class XMusicCd
{
private:
// You may not modify these member variables
std::string m_Author;
std::vector<XTrackInfo> m_TrackInfo;
public:
// You may not modify these member functions
XMusicCd() {}
XMusicCd(std::string Author, std::vector<XTrackInfo> Tracks):
m_Author(std::move(Author)),
m_TrackInfo(std::move(Tracks))
{}
void SetAuthor(std::string Author) { m_Author = std::move(Author); }
const std::string& GetAuthor() const { return m_Author; }
const std::vector<XTrackInfo>& GetTracks() const { return m_TrackInfo;}
void AddTrack(XTrackInfo NewTrack){
m_TrackInfo.emplace_back(std::move(NewTrack));
}
int GetLenght() const{
int sum=0;
auto && MyTracks = this->GetTracks();
for (auto && elem : MyTracks)
{
sum+= elem.GetTrackLength();
}
return sum;
}
};
class XCollection
{
private:
std::vector <XMusicCd> m_cdCollection;
public:
XCollection(){}
XCollection(std::vector <XMusicCd> CdCollection):
m_cdCollection(std::move(CdCollection))
{}
const std::vector <XMusicCd>& GetCds() const {return m_cdCollection;}
void addCD(XMusicCd NewCD){
m_cdCollection.emplace_back(std::move(NewCD));
}
};
//void CalcTotalLen(const XMusicCd& Cd){
// int sum = 0;
// auto && MyTracks = Cd.GetTracks();
// for (auto && elem : MyTracks)
// {
// sum+= elem.GetTrackLength();
// }
// std::cout << "total len: "<< sum << std::endl;
//}
void PrintCollectionContents(const XCollection& Collection){
//std::cout << "Cd number : " << "1" << "\n";
std::cout << "Cd info : " << std::endl;
auto MyCds = Collection.GetCds();
for(auto && elem: MyCds){
std::cout << elem.GetAuthor() << std::endl;
//How can I acess the tracks of the particular cd ?
}
auto tracks = MyCds.GetTracks;
// for (unsigned i = 0; i < tracks.size(); ++i) {
// std::cout << tracks.at(i).GetTrackName() << std::endl;
// }
}
void PrintCdContents(const XMusicCd& Cd)
{
std::cout << "Author : " << Cd.GetAuthor() << "\n";
std::cout << "\n" << std::endl;
std::cout << "Track Info" << std::endl;
//std::vector<XTrackInfo> tracks = Cd.GetTracks();
auto && MyTracks = Cd.GetTracks();
// for (std::vector<XTrackInfo>::const_iterator it = tracks.begin(); it != tracks.end(); ++it) {
// std::cout << it->GetTrackName() << std::endl;
// std::cout << it-> GetTrackLength() << std::endl;
// }
// std::vector<XTrackInfo> tracks = Cd.GetTracks();
// for (unsigned i = 0; i < tracks.size(); ++i) {
// std::cout << tracks.at(i).GetTrackName() << std::endl;
// }
for (auto && elem : MyTracks)
{
std::cout << elem.GetTrackName() << std::endl;
std::cout << elem.GetTrackLength() << std::endl;
}
}
int main()
{
// You may not change this function
XMusicCd MyCd;
MyCd.SetAuthor("Hello World");
MyCd.AddTrack(XTrackInfo("This is a test", 100));
MyCd.AddTrack(XTrackInfo("This is a test 2", 200));
PrintCdContents(MyCd);
//CalcTotalLen(MyCd);
std::cout << "total cd lenght: " << MyCd.GetLenght()<< std::endl;
XMusicCd MyCd2;
MyCd2.SetAuthor("Hello World 2");
MyCd2.AddTrack(XTrackInfo("This is a test", 400));
MyCd2.AddTrack(XTrackInfo("This is a test 2", 500));
PrintCdContents(MyCd2);
//CalcTotalLen(MyCd);
std::cout << "total cd lenght: " << MyCd2.GetLenght()<< std::endl;
XCollection MyCollection;
MyCollection.addCD(MyCd);
MyCollection.addCD(MyCd2);
//MyCollection.size();
PrintCollectionContents(MyCollection);
}