错误 C228:“左边必须有 class/struct/union 几次
Error C228: left of "must have class/struct/union several times
我们正在尝试使用以下代码序列化列表:
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 };
我们正在使用 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;
但是我们得到以下错误:
第 31 行:错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
第 31 行:错误 C2143:语法错误:在“&”之前缺少“,”
第 35 行:错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
第 35 行:错误 C2143:语法错误:缺少“;”在 '&'
之前
第 31 行:错误 C2065:'messageStruct':未声明的标识符
第 33 行:错误 C2065:'m_msgRef':未声明的标识符
第 33 行:错误 C2228:'.user_name' 的左侧必须有 class/struct/union
类型是“'unknown-type'”
第 33 行:错误 C2228:“.length”的左侧必须有 class/struct/union
第 34 行:错误 C2065:'m_msgRef':未声明的标识符
第 34 行:错误 C2228:“.content”左侧必须有 class/struct/union
类型是“'unknown-type'”
第 34 行:错误 C2228:'.length' 的左侧必须有 class/struct/union
第 35 行:错误 C2758:'MessageSerializer::Message':必须在构造函数 base/member 初始化列表
中初始化
第 51 行:server2.cpp(51):参见 'MessageSerializer::Message'
的声明
第 35 行:致命错误 C1903:无法从先前的错误中恢复;停止编译
你知道我的问题是什么吗?
是否在MessageSerializer
之前包含或定义了Message
?
Message is defined after, is it a problem?
是的,这是一个问题。将 Message
的定义移动到 MessageSerializer
.
之前
简单来说; C++ 编译器需要在使用类型之前查看声明(对于指针和引用)或定义(对于值)。
来自cppreference的声明;
Declarations introduce (or re-introduce) names into the C++ program.
And 用于定义;
Definitions are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following... [list not applicable and redacted].
我们正在尝试使用以下代码序列化列表:
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 };
我们正在使用 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;
但是我们得到以下错误:
第 31 行:错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
第 31 行:错误 C2143:语法错误:在“&”之前缺少“,”
第 35 行:错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
第 35 行:错误 C2143:语法错误:缺少“;”在 '&'
之前
第 31 行:错误 C2065:'messageStruct':未声明的标识符
第 33 行:错误 C2065:'m_msgRef':未声明的标识符
第 33 行:错误 C2228:'.user_name' 的左侧必须有 class/struct/union
类型是“'unknown-type'”
第 33 行:错误 C2228:“.length”的左侧必须有 class/struct/union
第 34 行:错误 C2065:'m_msgRef':未声明的标识符
第 34 行:错误 C2228:“.content”左侧必须有 class/struct/union
类型是“'unknown-type'”
第 34 行:错误 C2228:'.length' 的左侧必须有 class/struct/union
第 35 行:错误 C2758:'MessageSerializer::Message':必须在构造函数 base/member 初始化列表
中初始化
第 51 行:server2.cpp(51):参见 'MessageSerializer::Message'
的声明
第 35 行:致命错误 C1903:无法从先前的错误中恢复;停止编译
你知道我的问题是什么吗?
是否在MessageSerializer
之前包含或定义了Message
?
Message is defined after, is it a problem?
是的,这是一个问题。将 Message
的定义移动到 MessageSerializer
.
简单来说; C++ 编译器需要在使用类型之前查看声明(对于指针和引用)或定义(对于值)。
来自cppreference的声明;
Declarations introduce (or re-introduce) names into the C++ program.
And 用于定义;
Definitions are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following... [list not applicable and redacted].