Help With Enum Type & Classes

This is a discussion on Help With Enum Type & Classes within the C++ Programming forums, part of the General Programming Boards category; I just started doing classes and need some help with the last part of this program. ================= 1) In my ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    Help With Enum Type & Classes

    I just started doing classes and need some help with the last part of this program.
    =================
    1) In my header file I have this enum type and code (I'm posting the hole ".h" file so you know what I have):

    Code:
    enum Category {RnB, HipHop, Soul}; 
    
    class Music
    {
    public:
         Music();
    
         Music(string, string, string, int);
    	
         void Set(string song, string album, string singer, int length);
    
         string songTitle() const;
         string cdTitle() const;
         string artistName() const;
         int PlayTime() const;
    
         bool Compare(Music song2) const;
     
         Category MusicType(Music genre) const;
    
    private:
         string title;
         string cd;
         string artist;
         int time;
    };
    2) Enum code in Implentation File:

    Code:
    Category Music::MusicType(Music genre) const
    {
                    switch (MusicType(genre))
                    {
    		case RnB: cout << "Genre: R & B";
    			break;
    		case HipHop: cout << "Genre: Hip Hop";
    			break;
    		case Soul: cout << "Genre: Soul";
    			break;
                    }// End of Switch
    }
    In my main program it outputs some info of two songs that I declared. Now, am I coding it right in the implementation file? Or should that code actually be in the main program and something else in the impl.file? If it's right what Im not able to do is call the enum type to output the genre for each song. How can I do that?

    In my main program I have declared these two:
    Code:
    int main()
    {
    	Music s1;
    	Music s2;
    	
    	s1.Set("Let's Stay Together", "The Definitive Greatest Hits", 
                                "Al Green", 3);
    	s2.Set("It's Bigger Than Hip Hop", "Let's Get Free", "Dead Prez", 4);
                    
                    cout <<...
    Last edited by FJ8II; 04-26-2007 at 02:49 AM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    The implement file for your Music class should aslo have all the code for all the public functions

    Code:
    std::string Music::songTitle() const
    {
       // code here for this function
    }
    You may have done this aleady, but I can only see an enum in the file ( unless that is code snippett ).

    Does the code compile when all the files are linked together?

    Oh, and use header gaurds in your header files.

    Code:
    #ifndef MUSIC_H
    #define MUSIC_H
    
    // class
    
    #endif
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    I have everything for the functions and all of them compile with no problems. When I added the enum function I started having errors on that but the other functions still compile and run.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    I just dont know how to call the enum function and output the genre info.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you're trying to output the actual enum values -- RnB, HipHop, Soul -- instead of their "enumerical" representations, you'll have to write your own function for that.

    For example:

    Code:
    enum eTest
    {
    	test_zero=0,
    	test_one=2,
    	test_two=4,
    	test_three=6
    };
    
    ...
    
    std::string & getEnum(eTest t)
    {
    	std::string *s = NULL;
    	switch(t)
    	{
    		case test_zero:
    			s = new std::string("test_zero");
    		break;
    		case test_one:
    			s = new std::string("test_one");
    		break;
    		case test_two:
    			s = new std::string("test_two");
    		break;
    		case test_three:
    			s = new std::string("test_three");
    		break;
    	}
    	return *s;
    }
    Just as an fyi, I usually use C over C++, so I don't use references a lot. If this is wrong in any way, I apologize, but I think this should give you an idea of what I mean.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Is there anyway that I could make it output what I have in the switch statement?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    At the basic level, enums behave somewhat like #define statements. They are represented as integers, hence the reason why you have to write a function to do the naming yourself. The first case is resolved in the code I posted as case 0, not case "test_zero".

    In short, no.

    One thing you could do is build a const array of strings, where the index would match up to the name of the enum.

    So an array of strings, eTest_table, at index test_zero would be "test_zero". That would make lookups much quicker. You still have to do the work yourself, though, of making the array and writing a function to do the lookup. There's no way to get the enum names any other way to my knowledge.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    ok thank you...I'll play around with my code see what I can do with this...

    other inputs would be much appreciated though....

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In this particular case I don't actually see what good you get from the enumeration. Music genre is basically string-type information (just like the song title), so why not store it like the rest and do away with long and clumsy switches? You are not going to be able to enumerate all music styles after all...

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    this is an assignment and I have to use an enum for three genre only. it's more of abasic program to get going.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    I coulnt solve this....

    Just to let y'all know I dont have to use a switch statement but I have to use enum. I was just trying out the switch and I also tried if statement but I had no luck with both.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is some confusion in the Music class. The MusicType function is supposed to return a Category (which is of the enum type), but it is entirely unclear why it takes a Music parameter (note that Music doesn't represent "music (genre)" but rather a "song". Shouldn't it return the genre of this Music (like the other functions)? In this case you'll just need a private Category member to store the genre in.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    that makes since........ok do i call the enum function in main the same way I called the others "s1.songTitle()" ?

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, so far the assignment seems to be just about using "getters" (and "setters").

    If you have any other problems, don't forget to post the most recent code.

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    thank u...I got it now...finally I can get me a couple of hours of sleep...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef'd enum specifying type
    By trillianjedi in forum C Programming
    Replies: 4
    Last Post: 05-27-2008, 01:31 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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