0

I am trying to initialize a CRect via a macro so I can use x,y coordinates and getting C7555 error in VS2022 MFC. What have I missed please? BTW y,cy are inverted!

#define SETMYRECT( x , y , cx , cy ) { .left = x  , .top = y , .right = cx , .bottom = cy };
CRect ARect;
ARect = SETMYRECT( 0 , 0 , 50 , 50 );

Many thanks in advance imk

fabian
  • 80,457
  • 12
  • 86
  • 114
imk
  • 9
  • 4
  • 1
    Can't you type `ARect = CRect{ 0 , 0 , 50 , 50 };` instead? – drescherjm Mar 20 '22 at 12:43
  • 1
    Designated initializers are a C++20 feature. Do you compile with that version? Also why the double semicolon? – fabian Mar 20 '22 at 12:46
  • True i don't need the other semicolon was just testing it out. Not sure what C++ compiler version used by VS2020, but was a solution i found here and though it my work. https://stackoverflow.com/questions/62360478/using-define-macro-to-initialize-structure-object-in-c – imk Mar 20 '22 at 12:55

1 Answers1

2

The error C7555 expands to:

error C7555: use of designated initializers requires at least '/std:c++20'

The Windows Desktop Wizard in Visual Studio 2022 defaults to C++14, so you'll have to change that to C++20 if you wish to take advantage of this language feature. To do so from the IDE, right-click the project, select Properties, and navigate to Configuration Properties -> General. You'll want to set C++ Language Standard to ISO C++20 Standard (/std:c++20).

With that change out of the way, the code compiles as desired.

Not that that's a good thing. There's literally no reason to introduce a preprocessor macro here at all. If you do need to construct a RECT (or CRect) from x, y, cx, and cy, just use a function. And if you do, at least make it work for any x and y (not just 0):

inline CRect make_rect(int x, int y, int width, int height) {
    return CRect{ x, y, x + width, y + height };
}

Or just use the appropriate CRect constructor:

inline CRect make_rect(int x, int y, int width, int height) {
    return CRect{ POINT{ x, y }, SIZE{ width, height } };
}
IInspectable
  • 46,945
  • 8
  • 85
  • 181