views:

32

answers:

3

I am creating some custom exception classes doing the following

class GXException
{
public:
    GXException(LPCWSTR pTxt):pReason(pTxt){};
    LPCWSTR pReason;
};

class GXVideoException : GXException
{
public:
    GXVideoException(LPCWSTR pTxt):pReason(pTxt){};
    LPCWSTR pReason;
};

When I created GXVideoException to extend GXException, I get the following error

1>c:\users\numerical25\desktop\intro todirectx\godfiles\gxrendermanager\gxrendermanager\gxrendermanager\gxexceptions.h(14) : error C2512: 'GXException' : no appropriate default constructor available
+2  A: 

You need to call your base class constructor inside your derived constructor's initializer list. Also since you are deriving from the base class you should not redeclare a second variable by the same name (pReason).

class GXException
{
public:
    GXException(LPCWSTR pTxt):pReason(pTxt){};
    LPCWSTR pReason;
};

class GXVideoException : GXException
{
public:
    GXVideoException(LPCWSTR pTxt)
    : GXException(pTxt)
    {}
};
Brian R. Bondy
O ok, I never knew this. So anytime my base class accepts arguments in it's constructor, I need to call it from the parent class ??
numerical25
@numerical25: Yes if you don't specify one it will try to call a base constructor with no parameters. In your case you don't have one hence your error.
Brian R. Bondy
A: 

You probably just need a default constructor:

class GXException
{
public:
    GXException() : pReason("") {};
    GXException(LPCWSTR pTxt):pReason(pTxt){};
    LPCWSTR pReason;
};

Or as Brian says, call the base constructor from your derived exception.

John Weldon
A: 

Brian's answer is correct, but I also find it helpful to define an 'inherited' type so that I don't have too many references to the parent class to maintain, in case the hierarchy changes.

class GXVideoException : GXException
{
private:
    typedef GXEception inherited;
public:
    GXVideoException(LPCWSTR pTxt)
    : inherited(pTxt)
    {}
};
Roddy