vg8open
vg8open

Reputation: 57

Initialize a vector inside a struct

I have the following struct:

struct MsgProperties
{
    DWORD               msgSize;
    std::vector<BYTE>   vbuffer;

    //-Constructor
    MsgProperties(DWORD A = 0) : msgSize(A){}
};

I want to use that struct with a c++ vector so this is what I've done:

std::vector<MsgProperties> ReadText;
BYTE buffer[MAX_BUFFER_SIZE];
DWORD bytesRead;
do
{
    bytesRead = myFile.Read(buffer, MAX_BUFFER_SIZE);
    ReadText.push_back(MsgProperties(bytesRead, std::vector<BYTE>((BYTE*)buffer, (BYTE*)buffer + bytesRead)));

} while (bytesRead > 0);

but I can't figure out how to make it work correctly. Can someone tell me what I am missing?

Upvotes: 0

Views: 621

Answers (1)

SergeyA
SergeyA

Reputation: 62563

Looks like you need another 2 constructors:

MsgProperties(DWORD A, const std::vector<BYTE>& vec) : msgSize(A), vbuffer(vec) {}
MsgProperties(DWORD A, std::vector<BYTE>&& vec) : msgSize(A), vbuffer(vec) {}

Alernatively, a single constructor would be good too:

MsgProperties(DWORD A, std::vector<BYTE> vec) : msgSize(A), vbuffer(std::move(vec)) {}

On a side note, I do not see why you need message size at all. the size of the vector is the message size.

Upvotes: 1

Related Questions