错误 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;

但是我们得到以下错误:

类型是“'unknown-type'”

类型是“'unknown-type'”

你知道我的问题是什么吗?

是否在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].