I read documentation, and I don't know what is missing.
When I try to print the array, the problem is always the same.
I'm not able to convert Xmusic CD type into a char, and I know that isn't suppose too.
For which I read, I thought the following instructions were able to do what I've want, however the compiler doesn't agree with me.
The complete code is here:Code:void PrintCdContents(const XMusicCd& Cd) { // Implement this function such that it compiles // Also, to be explicit, you may not add any tracks to the CD in here. That is not the purpose of this function. You may also not change the signature of the function. // You shall specifically not modify the CD object by calling any setters. //Cd. std::cout << "Author : " << Cd.GetAuthor() << "\n"; std::cout << "\n" << std::endl; std::cout << "Track Info" << std::endl; std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostreambuf_iterator<char>(std::cout)); std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostream_iterator<char>(std::cout, " ")); for (auto c : Cd.GetTracks()){ std::cout << c << ' '; } //Cd.GetTracks(); }
Code:#include <iostream> #include <vector> #include <string> // You may not modify XTrackInfo 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;} int GetLength() const; // Left incomplete on purpose; you will implement it later void AddTrack(XTrackInfo NewTrack){ //m_TrackInfo.emplace_back(NewTrack.GetTrackName(),NewTrack.GetTrackLength()); m_TrackInfo.emplace_back(std::move(NewTrack)); } // Add additional functions to this class as necessary to make it compile. }; void PrintCdContents(const XMusicCd& Cd) { // Implement this function such that it compiles // Also, to be explicit, you may not add any tracks to the CD in here. That is not the purpose of this function. You may also not change the signature of the function. // You shall specifically not modify the CD object by calling any setters. //Cd. std::cout << "Author : " << Cd.GetAuthor() << "\n"; std::cout << "\n" << std::endl; std::cout << "Track Info" << std::endl; std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostreambuf_iterator<char>(std::cout)); // std::copy(Cd.GetTracks().begin(), Cd.GetTracks().end(), std::ostream_iterator<char>(std::cout, " ")); // for (auto c : Cd.GetTracks()){ // std::cout << c << ' '; // } //Cd.GetTracks(); } 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); }
And what type is *Cd.GetTracks().begin()?
Hint: To easily find a type, you can do:
int x = *Cd.GetTracks().begin();
Or
template<typename T> void Debug(); // Don't implement
Debug<decltype(*Cd.GetTracks().begin())>();
The compiler (or linker) will yield error messages which will contain the exact types.
These are beginner mistakes! You shouldn't be having trouble with these things if you're ready to implement this.
Originally Posted by marcoestevesThere is this so-called Telescope Rule: 'Thomson's Rule for First-Time Telescope Makers: "It is faster to make a four-inch mirror then a six-inch mirror than to make a six-inch mirror."'Originally Posted by marcoesteves
I have no idea if the rule is genuine, but the spirit of the rule applies here: it is faster/easier to learn by doing simpler projects and then do a more complex project than it is to do the more complex project.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That's fine but you need to take small steps, not giant leaps. Learn the basics then things won't seem as complicated.Yes, it is advanced, but I want to reach it
IMO your latest attempt is not what you should be trying. You should be iterating through the vector with for(;;) loops not using iterators. Iterators are not the answer to every programming problem and this problem can be solved easier without their use. And as I said in my last post you need to slow down, backup and learn the basics. Start with a simpler "project" like the one I introduced in my last post. Once you understand how to access a class from within a class switching to using the vector is very very simple. But until you really understand how classes interact you'll probably never really understand what is happening and your chances for success will be doubtful.
Again in this instance I don't recommend using iterators until you actually understand how to access a single instance of your Track class.
Jim
I disagree. Iterators are the common way of iterating through every standard container. Therefore it should follow that, unless you have need of indices, it should be the standard way of doing it and the first thing to learn and try. It's not harder. It's simpler because it also reduces the syntax and clutter with range-based for loops.
IMO the problem here is not trying to iterate through the vector but accessing elements of the "embedded" class. Until the OP knows how to access a single instance of the "Track" class from the "CD" class he'll never be able to access the elements of the vector, using any method.I disagree. Iterators are the common way of iterating through every standard container.
And IMO it's better to first learn to access individual elements of the vector using loops without the iterators, once this concept is learned iterators become much simpler.
Jim
This is what I felt.
I've started learning c++ a couple months. And first books and examples that I look into didn't mentioned STL, however with Elysia help and digging on another books I found out how powerfull it is.
And my current problem it is : I don't know how to acess the content of the TrackInfo inside of CD class. This is a composition problem
Well, like we've said, you need to take a step back and do some proper exercises in beginner books. A good list of recommended books can be found here.
Thanks, but most of them are not using c++11 standard.
I think this is another problem. The first type that I use c++ was in 2004. I did 2 or 3 simple projects(simple inheritance, simple user interface).
Since 2004, I have never seen C++ again, and lots of things have changed. I need to learn from "beggining", however some begginer concepts are too beginner, and some intermediate concepts are too hard for me. It's hard find a book which fits my needs :\
Last edited by marcoesteves; 09-03-2014 at 09:14 AM.