friends class lil prblm with enum

This is a discussion on friends class lil prblm with enum within the C++ Programming forums, part of the General Programming Boards category; Code: // tv.h Pg 733 prblm #1 #ifndef _TV_H_ #define _TV_H_ class Tv { friend class Remote; public: // Remote ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    friends class lil prblm with enum

    Code:
    // tv.h Pg 733 prblm #1
    #ifndef _TV_H_
    #define _TV_H_
    
    class Tv
    {   
        friend class Remote;
        public:
            // Remote can access TV private parts
            enum {Off, On};
            enum {MinVal,MaxVal=20};
            enum {Antenna,Cable};
            enum {TV,VCR};
            
            Tv(int s=Off, int mc=100): state(s), volume(5), maxchannel(mc),
                    channel(2), mode(Cable), input(TV){}
            void onoff(){state=(state==On)? Off: On;}
            bool ison() const {return state==On;}
            bool volup();
            bool voldown();
            void chanup();
            void chandown();
            
            void set_mode() {mode=(mode==Antenna)? Cable:Antenna;}
            void set_input() {input=(input==TV)? VCR :TV;}
            void settings() const; // display all settings
            bool Tv::set_state(Remote & r);    
        private:
            int state;
            int volume;
            int maxchannel;
            int channel;
            int mode;
            int input;
    };
    
    class Remote
    {
        friend class Tv;
        private:
            int mode;
            int state;
        public:
            enum{Normal,Interactive};
            Remote(int m=Tv::TV, int s=Normal): mode(m), state(s){}
            bool volup(Tv & t) {return t.volup();}
            bool voldown(Tv & t) {return t.voldown();}
            void onoff(Tv & t) {t.onoff();}
            void chanup(Tv & t) {t.chanup();}
            void chandown(Tv & t) {t.chandown();}
            void set_chan(Tv & t, int c) {t.channel=c;}
            void set_mode(Tv & t) {t.set_mode();}
            void set_input(Tv & t) {t.set_input();}
            void settings() const {cout << "Remote is in "
                    << (state==Normal? "Normal" : "Interactive")<< " mode.\n";}
    };
    
    
    inline bool Tv::set_state(Remote & r) 
    {
        if (ison())
        {
            r.state=(r.state==Normal)? Interactive:Normal;//errors here
            return 1;
        }    
        else
            return 0;
        
    };
    #endif
    errors
    Building Makefile: "C:\Dev-C++\Makefile.win"
    Executing make clean
    rm -f tv.o use_tv.o tv.exe

    g++.exe -c tv.cpp -o tv.o -I"C:\Dev-C++\include" -I"C:\Dev-C++\include\g++-3" -I"C:\Dev-C++\include" -g3

    In file included from tv.cpp:4:
    tv.h: In method `bool Tv::set_state(Remote &)':
    tv.h:63: `Normal' undeclared (first use this function)
    tv.h:63: (Each undeclared identifier is reported only once
    tv.h:63: for each function it appears in.)
    tv.h:63: `Interactive' undeclared (first use this function)

    make.exe: *** [tv.o] Error 1

    Execution terminated
    it seems my problem is the enumeration type not being in scope and that the easy way to fix it would be to add the enumeration type to the tv class but this seems redundant. One of the requirements of the problem is to add a state variable member to the Remote class that describes whether the remote control is in normal or interactive mode. How should I fix this error?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The easiest way is just to use a bool to represent the state, on (true) or off (false). Alternatively, you could name the enum.

    enum DoubleState { ON, OFF };

    and then make state a DoubleState instead of an int.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    There is already a bool function in the Tv class that determines weather it is on or off. Besides adding the state variable member to the Remote class that reflects whether the remote control is in normal or interactive (hence the enum in the remote class and the state variable) mode the problem says provide the Tv class with a mdthod for toggling the new Remote member. This method should work only if the Tv is in the on state hence my inline function at the end of declarations (put there do to the friends remote and necessity of seeing the remote declaration before the definition of the inline function which uses a remote object).

    I hope you are able to understand what I am saying it is a lil confusing to me the problem seems to be that the inline function(set_state) prototyped in Tv cant see the enumeration {Normal, Interactive} but remote must have this enumeration in it to describe the state variable.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Fixed like the following.

    r.state=(r.state==Remote::Normal)? Remote::Interactive: Remote::Normal;
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    doh didn't even think of doing that I'll give it a shot thanks.
    Your the man silentStrike works fine now !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  5. Replies: 8
    Last Post: 10-02-2005, 12:27 AM

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