Thread: Class not working.. color...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Class not working.. color...

    Hi, I am getting an error in my class that I do not understand.
    The error message says

    Bow class.cpp In member function `void Bow::draw()':
    class.cpp `color' undeclared (first use this function)

    I am just being dumb as I really can't see the problem. I have delcared color as a private member, so why the error? Should I get more coffee? ( chuckle!) thanks in advance

    Code:
    class Bow				
    {
    private:					
          string m_color;		
          bool drawn;		
          int  numOfArrows;						 
    public:								 
         Bow( string Color);		
        ~Bow();
    					 
        void draw();					
        int fire();				
    };										
    
    // constructor inplementation for Bow class
    
    Bow::Bow ( string Color )
    {
        srand(time(0));
        m_color = Color;
        numOfArrows = 10;
        drawn = false;
    }
    
    // deconstructor for Bow class
    
    Bow::~Bow()
    {
    }
    
    // draw method implementation - draws the bow
    
    void Bow::draw()
    {
          drawn = true;
     		 
          cout << "The " << color << " has been drawn" << endl;
    }
    
    // fire method implementation - fires the bow if drawn
    
    int Bow::fire()
    {
        if  (!drawn )	// if not drawn
     		{
        cout << color << " has not be drawn and cannot fire" << endl;
    		 		
        return 0;
    }
        
        int score;
        
        score = rand() % ( 10 - 0 + 1 ) + 0;
        
        if ( score == 0 )
        
        cout << color << " missed the target!" << endl;
        
        else
        
        cout << color << " scored " << score << " points!!" << endl;
        
        return score;
    }
    
    void bowTest ( void );	// function prototype
        				
    // driver -- main 
    
    int main ( void )
    {
           bowTest();
     		
           return 0;
    }
    
    // text challis - checks if class works
    
    void bowTest ( void )
    {
         Bow yellow ("yellow");
         yellow.fire();
         yellow.draw();
         yellow.fire();
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    EDIT: its ok, I solved it! I needed more coffee! lol!

    I forgot to tell the compiler that color was a member, using m_ duh! I do make some silly mistakes! Thanks for looking anyway guys! -pete

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Glad you fixed it. Naming conventions are for the human reader's benefit only - the compiler couldn't care less how you name your member variables, as long as the syntax is legal, and doesn't clash with other names

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Why isn't my class working?
    By Queatrix in forum C++ Programming
    Replies: 12
    Last Post: 09-17-2006, 03:47 PM
  5. trying to set data for a node class, not working?
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2005, 09:56 AM