C++带模板的数据包生成器

发布于 2024-09-30 15:44:01 字数 1246 浏览 0 评论 0原文

PacketBuilder 是一个小类,允许写入 char* 数组。追加函数:

template <class T>
void PacketBuilder::Append(const T value)
{
 memcpy((&m_Buffer) + m_Index, (const void*) &value, sizeof(T));
 m_Index += sizeof(T);
}

编译无错误。如果我调用 Append 并使用 T 作为无符号短整型 (WORD)。效果很好。如果我使用 T 作为无符号字符。我收到链接器错误。

m_Builder.Append<unsigned char>(0x01); // Error: LNK1120
m_Builder.Append<unsigned short>(0x0001); // Works

来自 VS2010 的错误(对不起,我有德语 vs2010):

错误 LNK2019:Verweis auf nicht aufgelöstes externes 符号“”公共: 无效__thiscall PacketBuilder::Append(无符号字符)" (??$Append@E@PacketBuilder@@QAEXE@Z)" 在 Funktion ""public: void __thiscall 中 客户端::DoHandshake(void)" (?DoHandshake@Client@@QAEXXZ)”。 1>C:\XXX\C++\SilkroadEmu\Debug\LoginServer.exe : 致命错误 LNK1120: 1 nicht aufgelöste externe Verweise。

翻译成英文:

错误 LNK2019:无法解析的外部 符号“”public:void __thiscall PacketBuilder::Append(无符号字符)" (??$Append@E@PacketBuilder@@QAEXE@Z)" 在函数“”public中:void __thiscall 客户端::DoHandshake(void)" (?DoHandshake@Client@@QAEXXZ)”。 1>C:\XXX\C++\SilkroadEmu\Debug\LoginServer.exe : 致命错误 LNK1120: 1 未解决 外部符号。

PacketBuilder is a little Class which allow to write into a char* array. The Append Functions:

template <class T>
void PacketBuilder::Append(const T value)
{
 memcpy((&m_Buffer) + m_Index, (const void*) &value, sizeof(T));
 m_Index += sizeof(T);
}

Compiling without errors. If I call Append and use T as unsigned short (WORD). It works great. If I use T as unsigned char. I get an Linker Error.

m_Builder.Append<unsigned char>(0x01); // Error: LNK1120
m_Builder.Append<unsigned short>(0x0001); // Works

Error from VS2010 (sry i got german vs2010):

error LNK2019: Verweis auf nicht
aufgelöstes externes Symbol ""public:
void __thiscall
PacketBuilder::Append(unsigned char)"
(??$Append@E@PacketBuilder@@QAEXE@Z)"
in Funktion ""public: void __thiscall
Client::DoHandshake(void)"
(?DoHandshake@Client@@QAEXXZ)".
1>C:\XXX\C++\SilkroadEmu\Debug\LoginServer.exe
: fatal error LNK1120: 1 nicht
aufgelöste externe Verweise.

Translated to English:

error LNK2019: Unresolved external
symbol ""public: void __thiscall
PacketBuilder::Append(unsigned char)"
(??$Append@E@PacketBuilder@@QAEXE@Z)"
in Function ""public: void __thiscall
Client::DoHandshake(void)"
(?DoHandshake@Client@@QAEXXZ)".
1>C:\XXX\C++\SilkroadEmu\Debug\LoginServer.exe
: fatal error LNK1120: 1 unsresolved
external symbol.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2024-10-07 15:44:01

将方法定义放在标头(hpp 文件)中,而不是放在实现(cpp)文件中。

Put the method definition in the header (hpp file), not in the implementation (cpp) file.

┊风居住的梦幻卍 2024-10-07 15:44:01

据我所知,您的 PacketBuilder 不是类模板。然而,PacketBuilder::Append 是一个模板方法,这要求它的定义在此方法的任何实例化点都必须可见。确保这一点的唯一真正安全的方法是将此方法模板的完整定义放入头文件中:

class PacketBuilder {
 // declarations of non-template members

public:
 template <class T> 
 void Append(const T value) 
 { 
  memcpy((&m_Buffer) + m_Index, (const void*) &value, sizeof(T)); 
  m_Index += sizeof(T); 
 }

};

Your PacketBuilder is not a class template, as far as I can see. PacketBuilder::Append is however a template method, which requires that it's definition must be visible at any point of instantiation of this method. The only really safe way to assure this is to put the complete definition of this method template into the header file:

class PacketBuilder {
 // declarations of non-template members

public:
 template <class T> 
 void Append(const T value) 
 { 
  memcpy((&m_Buffer) + m_Index, (const void*) &value, sizeof(T)); 
  m_Index += sizeof(T); 
 }

};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文