private vectors of classes

This is a discussion on private vectors of classes within the C++ Programming forums, part of the General Programming Boards category; if I have a class... Code: class Note { private: string step; string octave; string duration; int measurenum; }; with ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    private vectors of classes

    if I have a class...

    Code:
    class Note {
    private:
    
        string step;
        string octave;
        string duration;
        int measurenum;
    
    };
    with appropriate accessor functions and so forth, and then build a class...

    Code:
    class Measure{
    private:
        
        vector <Note> Measures;
    
    };
    what is the best method to access the variables in each instance of the vector( presumably step, octave, duration...) I am having trouble being able to make a method in the class to print out this information...Also, i am getting an error message stating "Note" not declared in this scope....Both files include each other and I have includes for <vector> as well as the std namespace...why is there an error?

    any help would be great...

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    sorry by "Note" not being declared in this scope, I am referring to the declaration of vector<Note> Measures...that is where the error is occuring

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Both files shouldn't include the other. Measure needs to know about Note, but as far as I can see, Note doesn't need to know about Measure.

    Note with a typical getter would look like this:

    Code:
    class Note {
    public:
        string get_step() const { return step; }
        //other methods
    private:
    
        string step;
        string octave;
        string duration;
        int measurenum;
    
    };
    If you only want access to the members so that you can output them, you could also consider overloading operator<<, so that the Note would know how to output itself.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by greatunknown
    what is the best method to access the variables in each instance of the vector( presumably step, octave, duration...)
    This depends on what exactly is the interface that you wish the Note class would provide. A very basic one would involve getter member functions corresponding to each member variable, possibly also with corresponding setter functions.

    Quote Originally Posted by greatunknown
    I am having trouble being able to make a method in the class to print out this information.
    What did you try?

    Quote Originally Posted by greatunknown
    Also, i am getting an error message stating "Note" not declared in this scope....Both files include each other and I have includes for <vector> as well as the std namespace...why is there an error?
    You cannot actually have mutual inclusion of files. However, there is no need to include the header that contains the definition of Measure in the header that contains the definition of Note. In fact, it looks like the header that contains the definition of Note only needs to include <string>, whereas the header that contains the definition of Measure should include <vector> and the header that contains the definition of Note.

    By the way, it looks like you are using using directives and/or using declarations in global/namespace scope in your header files. Do not do this. Instead, qualify string as std::string, vector as std::vector, etc. If you wish to use using directives/declarations, use them in a restricted scope and/or in your source files.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Wow yeah, no more error...guess I didnt need the include "measure.h" in note...Thanks!


    about pulling out the information from the vector, I guess Im asking exactly what the syntax is...I tried writing a Display() method that loops through the vector...example

    Code:
    for(i = 0 to Measures.size() ){
    
    cout<< Measures[i].getOctave();
    
    etc....
    
    }
    but that did not work...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Eh, is that your actual syntax? If so, you need to learn how to use a for loop, e.g., re-read the book/tutorial with which got you started on C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    No it is not its actually
    Code:
    for(i=0; i<Measures.size(); i++)
    sorry

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Right. I suggest that you post what you actually wrote, and also post the error message. Otherwise, the next problem that we point out might actually be a non-problem, whereas the real problem requires psychic powers to determine.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    sorry I thought you had those powers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 03:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  4. Vectors + Classes = Confusing
    By Epo in forum C++ Programming
    Replies: 59
    Last Post: 12-18-2004, 04:42 PM
  5. program to unpack packed data
    By vsk in forum C Programming
    Replies: 5
    Last Post: 11-14-2002, 09:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21