1
votes

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?

1
Maybe you should say which lines the errors are on?user253751
Did you #include <string> and using namespace std; ?Ian Thompson
Thank you very much. I have already modified the code so that you can see the line numbers and which lines the errors are on. We did #include <string> and using namespace std, so that shouldn't be the problem, but thank you anyway!Fátima Villa González
Is Message included or defined before MessageSerializer?Niall
Message is defined after, is it a problem?Fátima Villa González

1 Answers

2
votes

Is Message included or defined before MessageSerializer?

Message is defined after, is it a problem?

Yes, that is a problem. Move the definition of Message to before MessageSerializer.

In simple terms; the C++ compiler requires to see a declaration (for pointers and references) or a definition (for values) of a type before it is used.

From the cppreference for declarations;

Declarations introduce (or re-introduce) names into the C++ program.

And for definitions;

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].