简体   繁体   中英

How to initialize arrays and vectors in old extension clang and gcc compilers in macOS?

I have a code in c++. It works in windows, Ubuntu and Fedora. Now I want to compile it for macOS. I encountered some errors like

error: expected expression
error: variable-sized object may not be initialized

I have many lines like

int A[n][2] = {{2, 5},{2, 6},{3, 4},{3, 7},{4, 3},{5, 2},{6, 2},{7, 3}};
int B[n][2] = {{8, 15},{8, 16},{9, 14},{9, 17},{10, 13},{10, 14},{10, 17},{11, 12},{11, 15},{11, 16},{12, 11},{13, 10},{14, 9},{14, 10},{15, 8},{15, 11},{16, 8},{16, 11},{17, 9},{17, 10}};

I searched it is because of the old installed clang and gcc extension in macOS (I am using Mojave 10.14.0)

I do not know what can I do to resolve this problem.

You can define these instead like this:

int A[][2] = {{2, 5},{2, 6},{3, 4},{3, 7},{4, 3},{5, 2},{6, 2},{7, 3}};
int B[][2] = {{8, 15},{8, 16},{9, 14},{9, 17},{10, 13},{10, 14},{10, 17},{11, 12},{11, 15},{11, 16},{12, 11},{13, 10},{14, 9},{14, 10},{15, 8},{15, 11},{16, 8},{16, 11},{17, 9},{17, 10}};

Where clang is then happy.

If you specify n then you need to ensure the initializer list can fit in the defined structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM