我正在创建一些自定义异常类,执行以下操作
class GXException
{
public:
GXException(LPCWSTR pTxt):pReason(pTxt){};
LPCWSTR pReason;
};
class GXVideoException : GXException
{
public:
GXVideoException(LPCWSTR pTxt):pReason(pTxt){};
LPCWSTR pReason;
};
当我创建GXVideoException以扩展GXException时,我收到以下错误
1>c:\users\numerical25\desktop\intro todirectx\godfiles\gxrendermanager\gxrendermanager\gxrendermanager\gxexceptions.h(14) : error C2512: 'GXException' : no appropriate default constructor available
答案 0 :(得分:4)
您需要在派生构造函数的初始化列表中调用基类构造函数。此外,由于您是从基类派生的,因此不应使用相同的名称(pReason
)重新声明第二个变量。
class GXException
{
public:
GXException(LPCWSTR pTxt):pReason(pTxt){};
LPCWSTR pReason;
};
class GXVideoException : GXException
{
public:
GXVideoException(LPCWSTR pTxt)
: GXException(pTxt)
{}
};
答案 1 :(得分:1)
Brian的答案是正确的,但我也发现定义一个'继承'类型很有帮助,这样我就不会有太多的父类引用来维护,以防层次结构发生变化。
class GXVideoException : GXException
{
private:
typedef GXEception inherited;
public:
GXVideoException(LPCWSTR pTxt)
: inherited(pTxt)
{}
};
答案 2 :(得分:0)
您可能只需要一个默认构造函数:
class GXException
{
public:
GXException() : pReason("") {};
GXException(LPCWSTR pTxt):pReason(pTxt){};
LPCWSTR pReason;
};
或者正如Brian所说,从派生的异常中调用基础构造函数。