我正在用 C++ 制作一个 IRC 机器人,它应该对 IRC 频道中的消息做出反应。IRC频道属于多人游戏服务器,人们可以在游戏内与IRC进行交流,反之亦然。(基本上你可以看到服务器上发生的事情,包括聊天,而实际上不必在游戏中)。
我设置了一个非常基本的类层次结构来逐渐表示更具体的 IRC 事件类型。在图表上表示它看起来像这样
现在,我正在尝试在 C++ 中实现这种多级继承,就像这样(请原谅我不一致的样式。我也省略了 CIrcCommand 因为它与 CIngameCommand 非常相似,并且有相同的问题)
事件
class CIrcEvent
{
protected:
EventType m_type;
private:
void(*HandlerFunc)(std::string& nick, std::string& chan, std::vector<std::string>& message);
public:
CIrcEvent(void(*handler_ptr)(std::string& nick, std::string& chan, std::vector<std::string>& message))
{
m_type = EventType::IRCEVENT;
HandlerFunc = handler_ptr;
}
};
指令
class CCommand : public CIrcEvent
{
protected:
std::string m_name;
int m_level;
public:
std::string& GetCommandName() { return this->m_name; }
int GetLevel() { return this->m_level; }
};
游戏命令
class CIngameCommand : public CCommand
{
public:
CIngameCommand(std::string& CmdName, int CmdLevel, void(*CmdFuncPointer)(std::string& nick, std::vector<std::string>& message))
{
m_type = EventType::IGCMD;
m_name = CmdName;
m_level = CmdLevel;
HandlerFunc = CmdFuncPointer;
}
void Call(std::string& nick, std::vector<std::string>& message) { HandlerFunc(nick, message); }
private:
void(*HandlerFunc)(std::string& nick, std::vector<std::string>& message);
};
但是在类 CIngameCommand 的构造函数定义中......
CIngameCommand(std::string& CmdName, int CmdLevel, void(*CmdFuncPointer)(std::string& nick, std::vector<std::string>& message))
{ // <-- error on this line
m_type = EventType::IGCMD;
... 我收到 C2280 错误。
IRCCommand.h(63): error C2280: 'CCommand::CCommand(void)': attempting to reference a deleted function
IRCCommand.h(43): note: compiler has generated 'CCommand::CCommand' here
我不确定我做错了什么。我知道我没有为 CCommand 类定义自定义构造函数,但这是因为与所有其他构造函数相反,我只打算使用 CCommand 的派生类(CIngameCommand 和 CIrcCommand),我永远不会使用它在其自己的。
我能做些什么来规避这个错误?我的设计有缺陷吗?如果是这样,我如何以不同的方式实现这一点?
删除的函数是 的默认构造函数CIrcEvent
。它有一个用户定义的构造函数,然后必须从派生类调用该构造函数。
您没有在CCommand
so "compiler has generated 'CCommand::CCommand'" 中定义任何构造函数。这样一个编译器生成的构造函数也会调用基类的默认构造函数——但基类没有!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句