のためのおもちゃのコードは非常に簡単ではありません。コンパイル時知られている配列サイズ:グラムを使用してコンパイルを渡さ++が、ICPC
#include <stdio.h>
#include <math.h>
#define A 10
#define SIZE (int)(ceil(A/2)) // needs computation but known at compile-time
struct node {
int keys[SIZE];
};
int main() {
node a_node;
for (int i = 0; i < SIZE; ++i) {
a_node.keys[i] = i;
printf("%d\n", a_node.keys[i]);
}
return 0;
}
私はg++ -std=c++11 -O3 test.cpp -o test
を使用して、それをコンパイルすると予想されるとして、それが通過すると実行されます。しかしicpc -std=c++11 -O3 test.cpp -o test
インテルコンパイラを使用して、エラーが発生します。
test.cpp(8): error: function call must have a constant value in a constant expression int keys[SIZE];
を、それは関数呼び出しとみなされているのはなぜ?しかし、マクロで単純な算術演算(例:#define SIZE (A/2)
)を使用すると、エラーは消えます。
gccバージョン:4.8.5; iccバージョン:16.0.3。
どのようにこの問題を解決する手がかりですか?ありがとう。 int keys[SIZE]
がで宣言されている場合
------------------ ------------------は
を追加しました
#include <stdio.h>
#include <math.h>
#define A 10
#define SIZE (int)(ceil(A/2))
/*struct node {
int keys[SIZE];
};*/
int main() {
//node a_node;
int keys[SIZE];
for (int i = 0; i < SIZE; ++i) {
keys[i] = i;
printf("%d\n", keys[i]);
}
return 0;
}
gnuコンパイラとintelコンパイラの両方を渡します。非常に興味深く、厄介です。私は私が気づいていない間違いをしているかどうかはわかりません。
CまたはC++でコーディングしていますか? 'C++'タグは疑わしいです。 –