There is no suitable default constructor. (when creating a child class)

I create some custom exception classes by 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 a GXVideoException for the GXException extension, 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
+3
source share
3 answers

You need to call the base class constructor inside the list of initializers of the derived constructor. Also, since you are leaving the base class, you should not override the second variable with the same name ( pReason).

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

class GXVideoException : GXException
{
public:
    GXVideoException(LPCWSTR pTxt)
    : GXException(pTxt)
    {}
};
+4
source

, "" , , .

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

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.

0
source

Source: https://habr.com/ru/post/1750677/


All Articles