We are trying to serialize a list by using this code:
28 class MessageSerializer
29 {
30 public:
31 MessageSerializer(const Message& messageStruct)
32 : m_msgRef(messageStruct)
33 , m_userLength(m_msgRef.user_name.length())
34 , m_msgLength(m_msgRef.content.length())
35 {}
36
37
38 size_t RequiredBufferSize() const
39 {
40 return sizeof(int) + sizeof(size_t)*2 + m_msgLength +m_userLength;
41 }
42
43 void Serialize(void* buffer) const
44 {
45 PushNum (buffer, m_msgRef.id);
46 PushString (buffer, m_msgRef.user_name.c_str(), m_userLength);
47 PushString (buffer, m_msgRef.content.c_str(), m_msgLength);
48
49 }
50 private:
51 const Message& m_msgRef;
52 const size_t m_msgLength;
53 const size_t m_userLength;
54
55 template<typename INTEGER>
56 void PushNum(void*& buffer, INTEGER num) const
57 {
58 INTEGER* ptr = static_cast<INTEGER*>(buffer);
59 //copying content
60 *ptr = num;
61 //updating the buffer pointer to point the next position to copy
62 buffer = ++ptr;
63 }
64 void PushString(void*& buffer, const char* cstr, size_t length) const
65 {
66 PushNum(buffer, length);
67 //copying string content
68 memcpy(buffer, cstr, length);
69 //updating the buffer pointer to point the next position to copy
70 char* ptr = static_cast<char*>(buffer);
71 ptr += length;
72 buffer = ptr;
73 }
74 };
And we are using the struct
:
struct Message {
static unsigned int s_last_id; // keep track of IDs to assign it automatically
unsigned int id;
string user_name;
string content;
Message(const string& a_user_name, const string& a_content) :
user_name(a_user_name), content(a_content), id(++s_last_id)
{
}
Message(){}
};
unsigned int Message::s_last_id = 0;
However we get the following errors:
Line 31: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Line 31: error C2143: syntax error : missing ',' before '&'
Line 35: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Line 35: error C2143: syntax error : missing ';' before '&'
Line 31: error C2065: 'messageStruct' : undeclared identifier
Line 33: error C2065: 'm_msgRef' : undeclared identifier
Line 33: error C2228: left of '.user_name' must have class/struct/union
type is ''unknown-type''
Line 33: error C2228: left of '.length' must have class/struct/union
Line 34: error C2065: 'm_msgRef' : undeclared identifier
Line 34: error C2228: left of '.content' must have class/struct/union
type is ''unknown-type''
Line 34: error C2228: left of '.length' must have class/struct/union
Line 35: error C2758: 'MessageSerializer::Message' : must be initialized in constructor base/member initializer list
Line 51: server2.cpp(51) : see declaration of 'MessageSerializer::Message'
Line 35: fatal error C1903: unable to recover from previous error(s); stopping compilation
Do you know what my problem is?
Message
included or defined beforeMessageSerializer
? – Niall