Inheritance issue

This is a discussion on Inheritance issue within the C++ Programming forums, part of the General Programming Boards category; I'm trying to run this line of code: Code: m_brush = new ConstantBrush(m_brush->m_color, m_brush->m_flow, m_brush->m_radius); m_brush is of type Brush, ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    Inheritance issue

    I'm trying to run this line of code:
    Code:
    m_brush = new ConstantBrush(m_brush->m_color, m_brush->m_flow, m_brush->m_radius);
    m_brush is of type Brush, which is a superclass to ConstantBrush. I'm getting an error:
    Code:
    error C2061: syntax error : identifier 'ConstantBrush'
    I've looked into the error with not much luck. I was wondering if someone knows what's going on.

    Any help would be great, thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Perhaps you forgot to include the relevant header file?
    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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    Everything seems its where it should be for headers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Then show us your code, if you will.
    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. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    The first four are the headers, the last is where I'm trying to implement it.

    Code:
    /**
     * \author CSci5107 Guru
     *
     * \file  BrushApp.h
     * \brief Main application class for the Brush assignment
     *
     */
    
    
    #ifndef BRUSHAPP_H
    #define BRUSHAPP_H
    
    #include "BaseGfxApp.h"
    #include "Brush.h"
    
    
    /** The BrushApp class.  This sets up the GUI and the drawing canvas.  You shouldn't
        need to modify anything here.  Instead, fill in your own MyBrushApp class.  You
        will however need to call setPixel, getPixel, and potentially getData to draw to
        the canvas. */  
    class BrushApp : public BaseGfxApp {
    public: 
    
        enum UIControlType {
        	UI_BRUSHTYPE = 0,
        	UI_COLOR_R = 1,
        	UI_COLOR_G = 2,
        	UI_COLOR_B = 3,
        	UI_FLOW = 4,
        	UI_RADIUS = 5,
        	UI_QUIT = 6
        };
    
        BrushApp(int argc, char* argv[]);
        virtual ~BrushApp();
    
    	/// Sets an individual pixel's data on this canvas
    	void setPixel(int x, int y, const ColorData& color);
    
    	/// Gets an individual pixel's data on this canvas
    	ColorData getPixel(int x, int y) const;
     
    	/// Returns a pointer to the array of RGB image data, so you can read/write directly
    	/// to it, which is faster than using setPixel/getPixel.
    	unsigned char* getData();
    
        
        // The following functions are pure virtual, these need to be implemented in a
        // MyBrushApp subclass.
        virtual void newBrushType(int brushType) = 0;
        virtual void newColor(unsigned char red, unsigned char green, unsigned char blue) = 0;
    	virtual void newRadius(int radius) = 0;
        virtual void newFlow(int rate) = 0;
        virtual void mousePressed(int x, int y) = 0;
    	virtual void mouseDragged(int x, int y) = 0;
    	virtual void mouseReleased(int x, int y) = 0;
    
    
    	void display();    
    	void reshape(int width, int height);
    	void gluiControl(int controlID);
        void mouseMotion(int x, int y);
    	void leftMouseDown(int x, int y);
    	void leftMouseUp(int x, int y);
    	
    private:
    
    	// This is the pointer to the buffer where the actual color data for the canvas
    	// that we paint on is stored.
    	unsigned char *m_buffer;
    
    	// These are used internally to handle events for the GUI
        int m_bt;
        int m_r;
        int m_f;
        int m_cr, m_cg, m_cb;
        bool m_drag;
        int m_width;
        int m_height;
    };
    
    
    #endif
    Code:
    /**
     * \author You!
     *
     * \file  MyBrushApp.h
     * \brief Your implementation of the Brush assignment
     *
     */
    
    
    #ifndef MYBRUSHAPP_H
    #define MYBRUSHAPP_H
    
    #include "BrushApp.h"
    #include "Brush.h"
    
    /** This is your implementation of the Brush application */
    class MyBrushApp : public BrushApp {
    public: 
    
        MyBrushApp(int argc, char* argv[]);
        virtual ~MyBrushApp();
    
    	/// Need to switch to using brushType (see Brush.h) for enum values
        void newBrushType(int brushType);
        
        /// Need to switch the brush color
        void newColor(unsigned char red, unsigned char green, unsigned char blue);
    
    	/// Need to set a new radius for the brush
    	void newRadius(int radius);
    
    	/// Need to set a new flow rate for the brush
        void newFlow(int rate);
    
    	/// This is called when the left mouse button is first pressed down
        void mousePressed(int x, int y);
    
    	/// This is called when the mouse moves and the left button is still held down
    	void mouseDragged(int x, int y);
    
    	/// This is called when the left mouse button is released
    	void mouseReleased(int x, int y);
    
    private:
    
    	/// Pointer to the current brush
    	Brush *m_brush; 
    
    };
    
    
    #endif
    Code:
    /**
     * \author CSci5107 Guru
     *
     * \file  Brush.h
     * \brief Superclass for brushes
     *
     */
    
    #ifndef __BRUSH_H__	//header guards
    #define __BRUSH_H__
    
    #include "CommonInc.h"
    
    // An enum for brush types
    enum BRUSH_TYPE {
       BRUSH_CONSTANT, 
       BRUSH_LINEAR, 
       BRUSH_QUADRATIC,
       BRUSH_GAUSSIAN,
       BRUSH_SPECIAL
    };
    
    
    struct ColorData {
    	unsigned char r, g, b;
    };
    
    
    class BrushApp;		// forward declaration
    
    /// This is the superclass for brushes.
    class Brush {
    public:
    
    	/// Constructor for brush.  Pass it the initial color, flow and radius.
    	Brush(ColorData color, int flow, int radius);
    	
    	/// Destructor for brush.  Clean up all brush allocated resources.
    	virtual ~Brush();
    	
    	/// Set the brush color.
    	void setColor(ColorData color);
    	
    	/// Set the flow of the brush.
    	void setFlow(int flow);
    	
    	/// Set the radius of the brush.
    	void setRadius(int radius);
    
    	/// Apply the brush one time centered at (x,y) on the drawing "canvas" that is
    	/// part of brushApp.  Hint: Call brushApp->setPixel and getPixel to do the painting.
    	void paintOnce(int x, int y, BrushApp* brushApp);
    
    protected:
    
    	/// Pure virtual function.  You have to override this in subclasses
    	virtual void makeMask() = 0;
    	
    	// C++ coding convention is to put m_ in front of member variables
    	
    	/// This is a pointer to the brush mask.
    	float* m_mask;
    	
    	/// Color for the current brush.
    	ColorData m_color;
    	
    	/// The flow setting of the brush.
    	int m_flow;
    	
    	/// The radius setting of the brush.
    	int m_radius;
    };
    
    #endif // __BRUSH_H__
    Code:
    /**
     * \author CSci5107 Guru
     *
     * \file  ConstantBrush.h
     * \brief This is a constant brush.
     *
     */
    
    #ifndef CONSTANTBRUSH_H
    #define CONSTANTBRUSH_H
    
    #include "Brush.h"
    
    /// This is a constant brush with equal mask distribution in a circle.
    class ConstantBrush : public Brush {
    public:
    	ConstantBrush(ColorData color, int flow, int radius);
    	virtual ~ConstantBrush();
    
    protected:
    	void makeMask();
    };
    
    #endif
    Code:
    #include "MyBrushApp.h"
    
    
    MyBrushApp::MyBrushApp(int argc, char* argv[]) : BrushApp(argc, argv)
    {
    	
    	
    }
    
    MyBrushApp::~MyBrushApp()
    {
    }
    
    
    //Here's the function I'm getting my error on.
    void
    MyBrushApp::newBrushType(int brushType)
    {
    	cout << "newBrushType " << brushType << endl;
    	if (brushType == 0) {	//Constant
    		m_brush = new ConstantBrush(m_brush->m_color, m_brush->m_flow, m_brush->m_radius);
    	} else if(brushType == 1){	//Linear
    		m_brush = new LinearBrush(m_brush->m_color, m_brush->m_flow, m_brush->m_radius);
    	} else if(brushType == 2){	//Quadratic
    		m_brush = new QuadraticBrush(m_brush->m_color, m_brush->m_flow, m_brush->m_radius);
    	} 
    
    }
        
    void
    MyBrushApp::newColor(unsigned char red, unsigned char green, unsigned char blue)
    {
    	cout << "newColor " << (int)red << " " << (int)green << " " << (int)blue << endl;
    	ColorData brushColor;
    	brushColor.r = red;
    	brushColor.g = green;
    	brushColor.b = blue;
    	m_brush->setColor(brushColor);
    }
    
    void
    MyBrushApp::newRadius(int radius)
    {
    	cout << "newRadius " << radius << endl;
    	m_brush->setRadius(radius);
    }
    
    void
    MyBrushApp::newFlow(int rate)
    {
    	cout << "newFlow " << rate << endl;
    	m_brush->setFlow(rate);
    }
    
    // Tells us we should place one paint stroke 
    void 
    MyBrushApp::mousePressed(int x, int y)
    {
    	cout << "mousePressed " << x << " " << y << endl;
    	m_brush->paintOnce(x,y,this);
    }
    
    // Tells us we should place another single paint stroke
    void
    MyBrushApp::mouseDragged(int x, int y)
    {
    	cout << "mouseDragged " << x << " " << y << endl;
    }
    
    void
    MyBrushApp::mouseReleased(int x, int y)
    {
    	cout << "mouseReleased " << x << " " << y << endl;
    }
    If you need more, let me know.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    You're not including ConstantBrush.h anywhere, which was what we told you initially.
    Also, MyBrushApp::newBrushType seems to be allocating memory via new, but you never delete it. Memory leak.
    I'd advise you to use smart pointers.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple inheritance issue
    By Helix in forum C++ Programming
    Replies: 14
    Last Post: 03-31-2009, 02:21 PM
  3. Issue with pointers to an abstract class and inheritance
    By bainevii in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2009, 07:51 AM
  4. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  5. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM

Tags for this Thread


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