How to access Abstract class function


In my code i want to access some function from a Abstract class.I have created a pointer for that class but,

tResult CTest_QtFilter::OnPaint()
{
ICanvas* pCanvas ;

cString strText = cString::Format("DEMO DEMO SAMPLE %d", ++m_nSampleCounter);

/*if (State_Running != GetState())s
{
RETURN_NOERROR;
}
*/

tInt nParamX1;
tInt nParamY1;
tInt nParamX2;
tInt nParamY2;

tInt nR, nG, nB;

// Draw rectangles
for (tInt nI = 0; nI < m_nNumRects; nI++)
{
// Set canvas color
nR = RNDC;
nG = RNDC;
nB = RNDC;
pCanvas->Color(nR, nG, nB);

// Calculate random coordinates
nParamX1 = (tInt)((RNDX * m_fScaleX) +1.5);
nParamY1 = (tInt)((RNDY * m_fScaleY) + 0.5);
nParamX2 = (tInt)((RNDX * m_fScaleX) +1.5);
nParamY2 = (tInt)((RNDY * m_fScaleY) + 0.5);

// Draw to our canvas
pCanvas->DrawRect(nParamX1, nParamY1, nParamX2, nParamY2);
}
}
Here in Above code
ICanvas is an Abstract class.The class has the visiblity in my current function i have created a pointer for that class "ICanvas* pCanvas".but when i am trying to access "pCanvas->Color(nR, nG, nB);",i am getting a runtime error "The variable 'pCanvas'is being used without being initialized".

Icanvas class for reference
class ICanvas : public IGraphicsMode
{ public:

/**
* Returns the width of the canvas.
* @return The width (in pixel).
*/
virtual tInt GetWidth() const = 0;

/**
* Returns the height of the canvas.
* @return The height (in pixel).
*/
virtual tInt GetHeight() const = 0;

/**
* Returns the resolution of the canvas.
* @return The number of bits per pixel.
*/
virtual tInt GetBitsPerPixel() const = 0;

/**
* Define a rectangle for the screen mapping
* @param x1 [in] Upper left x coordinate
* @param y1 [in] Upper left y coordinate
* @param x2 [in] Bottom right x coordinate
* @param y2 [in] Bottom right y coordinate
*
* @return void
/**
* This function sets the current background color to col value.
* All next draw calls will draw with the col as background.
*
* @param nRed [in] Red value (0-255).
* @param nGreen [in] Green value (0-255).
* @param nBlue [in] Blue value (0-255).
* @param nAlpha [in] Alpha value (0-255 (non-full)).
*
* @return void
*/
virtual tVoid Color(tInt nRed, tInt nGreen, tInt nBlue, tInt nAlpha=0) = 0;

}
Firstly, please use code tags.

The error that are receiving is self explanatory - you have not initialized the class. Basically, you have declared a pointer to an abstract base class, but you haven't initialized that pointer to an allocated piece of memory for a valid implementation class. Make a class that derives from ICanvas and implements its pure virtual functions, and then declare the pointer *pCanvas as begin the location of an object of that derived class (usually through new, but if you do that don't forget to delete it again later.
ICanvas is the library header File which i am using for my code,So it unnecessay to inherit the class.
Would you suggest some other idea.
I tried calling the function using scoperesolution operator also but it did not worked.
So it unnecessay to inherit the class.


In that case, where are the implementations for your "Color" function (or any of the functions you have)? If you think about it, because this is an abstract class, there will not be any implementations for these functions (which is why the compiler can't generate objects of them). What you need to do is supply the pointer a class that does implement those functions, so that it can be done properly. Whether what is in header files or whatever doesn't really make a difference.

Here is what I mean (you might understand better if I show you in code):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Imagine this as "ICanvas"
class IAbstractBaseClass {
    public:
        // Virtual destructor for class (calls inherited classes destructor instead)
        virtual ~IAbstractBaseClass();

        // Two functions to be implemented
        virtual int add(int a, int b) = 0;
        virtual float divide(int a, int b) = 0;
};

// Define the class to implement the functions, inherit the base class
class ImplementationClass : public IAbstractBaseClass {
    public:
        ImplementationClass() {}
        ~ImplementationClass() {}

        int add (int a, int b) { return a + b; }
        float divide (int a, int b) { return a / b; }
};

int main() {
    IAbstractBaseClass* objptr;
    objptr = new ImplementationClass;

    std::cout << objptr->add(5, 6) << std::endl;
    std::cout << objptr->divide(11, 2) << std::endl;

    delete objptr;
    return 0;
}
Topic archived. No new replies allowed.