1

I want to declare an array inside a structure with a predefined constant size, but it gives me this error : expected a ']'.

#define MAX_SZAMJEGY 200;

struct szam {

    int szj[MAX_SZAMJEGY];
    bool negative;
};
2
  • 3
    remove ; from define Commented Nov 29, 2016 at 15:30
  • 2
    Or even better, replace the define with constexpr std::size_t MAX_SZAMJEGY = 200; Commented Nov 29, 2016 at 15:31

3 Answers 3

5

Macro expands to

int szj[200;]; 

which is not valid C++ code.

remove ; from #define MAX_SZAMJEGY 200;

Sign up to request clarification or add additional context in comments.

Comments

4

A preferred C++ solution is to use constants rather than macros. This way you will not have a semicolon problem, and it comes with tons of other benefits as well. Here is how:

(C++ 98):

static const size_t MAX_SZAMJEGY 200;

struct szam {

    int szj[MAX_SZAMJEGY];
    bool negative;
};

(C++11)

static constexpr size_t MAX_SZAMJEGY=200;

struct szam {

    int szj[MAX_SZAMJEGY];
    bool negative;
};

And while you are on it, and if you are using C++11, you might as well replace C-style array with C++ std::array. While it doesn't make too much of a difference, it is slightly more convenient to use.

Comments

2

try

#define MAX_SZAMJEGY 200

instead of

#define MAX_SZAMJEGY 200;

(the semicolon enters the macro)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.