P
P
Pavel2015-12-08 20:28:54
C++ / C#
Pavel, 2015-12-08 20:28:54

Why does the compiler swear at an array whose dimension is given by a static constant?

Hello, I declare the following structure:

struct Packet
{
    static const int bytes_for_content; //определяется в соотв-ем cpp файле
//...
    char content[bytes_for_content]; //это компилятору и не понравилось
};

Tell me what's wrong.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Ocelot, 2015-12-08
@Ocelot

The size of the fields of the structure is determined at compile time, and the value of the constant is assigned at runtime of the program, so it swears. Use #define for things like this.

O
Oleg Tsilyurik, 2015-12-08
@Olej

struct Packet
{
    static const int bytes_for_content; 
//...
    char content[bytes_for_content]; //это компилятору и не понравилось
};
const int Packet::bytes_for_content = 100;

Somehow the compiler should like it. ;-)

P
Pavel, 2015-12-09
@PavelG94

Thank you all for your participation, although in truth I did not fully understand this topic and used an anonymous enum in my program for constants.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question