Thread: Learning C++ by Example

  1. #76
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Where is "NewTrack" defined?
    Nowhere. NewTrack is a pointer to a vector. Probably I need to add something to the print function.
    Quote Originally Posted by Elysia View Post
    Where are the functions begin, end defined?
    std::vector ?
    the compiler says thar are not defined, probably because the compiler don't know NewTrack is a vector type

    Quote Originally Posted by Elysia View Post
    Where are the operators ++ (prefix), != and * defined?
    What the compiler say?
    They are not defined

    Quote Originally Posted by Elysia View Post
    You can create a function that returns all tracks or one track, if you will. You will get less encapsulation, though, and some programmers will come to expect to be able to iterate over tracks because... it's a standard pattern for all things that are iterable (all standard contains supports it).
    I think the best is return all tracks. Is it possible return all tracks an then create a functio to iterate through the tracks?

  2. #77
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    Nowhere. NewTrack is a pointer to a vector. Probably I need to add something to the print function.

    std::vector ?
    the compiler says thar are not defined, probably because the compiler don't know NewTrack is a vector type


    They are not defined
    NewTrack is nothing because it is not defined nor declared. My point is that you need to create that if you want it to work.
    std::vector does have these, but they are unique to std::vector. The point is that if you want your type to be iterable, you need to create these.

    I think the best is return all tracks. Is it possible return all tracks an then create a functio to iterate through the tracks?
    It is possible, sure.

    Are you really sure you are ready for this, though? I see so many beginner mistakes. I see so much confusion and problems where there really should be none. This is as simple as it gets, these last exercises.
    Have you considered going through beginner books for C++ and doing all the exercises while absorbing the theory?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #78
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post

    Are you really sure you are ready for this, though? I see so many beginner mistakes. I see so much confusion and problems where there really should be none. This is as simple as it gets, these last exercises.
    Have you considered going through beginner books for C++ and doing all the exercises while absorbing the theory?
    I want to learn.
    I'm reading a book too. c++ primer 5th edition.

    I know that are ways to create operators. I think this "example" is a nice way to learn, but it is messy too. There are lot of concepts to understand and learn.

  4. #79
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I know that are ways to create operators. I think this "example" is a nice way to learn, but it is messy too. There are lot of concepts to understand and learn.
    Yes, but the fact that you cannot implement a simple function to print the contents of a vector is worrying that you're trying to do too much at this point in time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #80
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Yes, but the fact that you cannot implement a simple function to print the contents of a vector is worrying that you're trying to do too much at this point in time.
    I know how to print a vector.
    I don't know how to access the TrackInfo vector inside Cd Class. There is something that I'm not catching.

    To print the data inside TrackInfo vector I need to access the data.
    I cannot create a simple accessor like:
    Code:
    const XTrackInfo GetTracks() const { return m_TrackInfo;}
    So, with my current knowlegde I cannot continue the implementation.

  6. #81
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    I cannot create a simple accessor like:
    Code:
    const XTrackInfo GetTracks() const { return m_TrackInfo;}
    So, with my current knowlegde I cannot continue the implementation.
    Come on! What is the type of m_TrackInfo?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #82
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Come on! What is the type of m_TrackInfo?
    XMusicCd ...
    So, I'm being a moron.

    Trackinfo is an array of XTrackInfo type, however XMusicCD owns.

  8. #83
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    Trackinfo is an array of XTrackInfo type, however XMusicCD owns.
    Yes, but that's not the point.
    The point is that the return type of
    Code:
    const XTrackInfo GetTracks() const { return m_TrackInfo;}
    does not match the actual type of m_TrackInfo.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #84
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Yes, but that's not the point.
    The point is that the return type of
    Code:
    const XTrackInfo GetTracks() const { return m_TrackInfo;}
    does not match the actual type of m_TrackInfo.
    It is a vector

    Theres is an update:
    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;
    
        Cd.GetTracks();
    
    //    for (auto it = NewTrack.begin(), end = NewTrack.end(); it != end; ++it)
    //    {
    //        std::cout << *it << " ";
    //    }
    
    
    }
    
    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);
    }
    At this moment I don't know which is the best: Create operators in order to use iterators or simply try to print the Tracks content .
    I think the best pratice is the the first one.

  10. #85
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Creating an iterator interface is the best practice. In this case, it is not that difficult, but you have to understand what iterators are and how they work first and then figure out how to make the class iterable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #86
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by Elysia View Post
    Creating an iterator interface is the best practice. In this case, it is not that difficult, but you have to understand what iterators are and how they work first and then figure out how to make the class iterable.
    Ok.
    I think that I need begin(), cbegin(), end() and ++ .
    Do I need create another class? A template?
    Or "just" add const and const iterator keywords?

  12. #87
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you trying to mess around with iterators if you can't figure out how to print the elements of the vector in the first place? Quit trying to add added complexity to the program until you understand the basics.

    I really think you need to start simpler. IMO it would be better to put the vector aside for now and just create one instance of your XTrackInfo in the XMusicCd class. Populate this single instance, then print the information. Once you demonstrate that you can work with a single instance, it should be easy to convert that knowledge to working with a vector.

    Try something like:
    Code:
    #include <iostream>
    #include <string>
    
    class Track
    {
    private:
       std::string name;
       int length;
    public:
       Track(std::string n, int len) : name(n), length(len) { /* Blank Body */ }
    
    };
    
    class Cd
    {
    private:
       std::string name;
          Track track;
    public:
       Cd(std::string author_name, std::string track_name, int length) :
          name(author_name), track(track_name, length) { /* Blank Body */ }
    };
    
    int main()
    {
       Cd cd("Big Bad Mamma", "Track 1", 10);
    
       /* Now create a function that will print the Authors name, the track name, and the track length.
          You'll need to create member functions to access the private data of the Track class.
          These functins could be either "getters" or an overloaded stream operator<<
          your choice. */
       cd.print();
    
    
       std::cout << "Finished" << std::endl;
       return 0;
    }


    Jim

  13. #88
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by marcoesteves View Post
    Ok.
    I think that I need begin(), cbegin(), end() and ++ .
    Do I need create another class? A template?
    Or "just" add const and const iterator keywords?
    I am going to agree with jimblumberg here. This project is just way too advanced for you at the moment.
    I strongly suggest you do basic exercises found in beginners' books for now. Feel free to chime in (in a new thread) if you have problems with THOSE.
    Learn how to work with most standard library types. How to iterate them, insert things, delete things, etc.
    THEN come back to this project.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #89
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also can you provide the Author, Publisher, and date of Publication of the book you are referring to?

    Jim

  15. #90
    Registered User
    Join Date
    Nov 2010
    Posts
    122
    Quote Originally Posted by jimblumberg View Post
    Why are you trying to mess around with iterators if you can't figure out how to print the elements of the vector in the first place? Quit trying to add added complexity to the program until you understand the basics.

    I really think you need to start simpler. IMO it would be better to put the vector aside for now and just create one instance of your XTrackInfo in the XMusicCd class. Populate this single instance, then print the information. Once you demonstrate that you can work with a single instance, it should be easy to convert that knowledge to working with a vector.

    Try something like:
    Code:
    #include <iostream>
    #include <string>
    
    class Track
    {
    private:
       std::string name;
       int length;
    public:
       Track(std::string n, int len) : name(n), length(len) { /* Blank Body */ }
    
    };
    
    class Cd
    {
    private:
       std::string name;
          Track track;
    public:
       Cd(std::string author_name, std::string track_name, int length) :
          name(author_name), track(track_name, length) { /* Blank Body */ }
    };
    
    int main()
    {
       Cd cd("Big Bad Mamma", "Track 1", 10);
    
       /* Now create a function that will print the Authors name, the track name, and the track length.
          You'll need to create member functions to access the private data of the Track class.
          These functins could be either "getters" or an overloaded stream operator<<
          your choice. */
       cd.print();
    
    
       std::cout << "Finished" << std::endl;
       return 0;
    }


    Jim
    I appreciate your advice, however I'm too stubborn to give up at least now.
    I'm trying do both things in parallel. Learn and Implemmenting.
    It is clear that I'm missing something, probably I didn't find any appeal books.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning C++
    By sLIVER in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2007, 08:18 PM
  2. Learning
    By nojoegohome in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2006, 04:33 AM
  3. Learning Dos and learning Windows
    By blankstare77 in forum C++ Programming
    Replies: 8
    Last Post: 07-31-2005, 03:48 PM
  4. Learning?
    By bob20 in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-11-2002, 10:17 AM

Tags for this Thread