C++ #定义“原因”;预期主表达式“;错误
但是当我将第7行更改为for(int I=0;I删除分号时,它编译得很好-分号包含在替换中 有时,让编译器只运行预处理器很有用C++ #定义“原因”;预期主表达式“;错误,c++,c-preprocessor,C++,C Preprocessor,但是当我将第7行更改为for(int I=0;I删除分号时,它编译得很好-分号包含在替换中 有时,让编译器只运行预处理器很有用 test-define.cpp: In function ‘int main()’: test-define.cpp:7:22: error: expected primary-expression before ‘;’ token test-define.cpp:7:22: error: expected ‘)’ before ‘;’ token test-defin
test-define.cpp: In function ‘int main()’:
test-define.cpp:7:22: error: expected primary-expression before ‘;’ token
test-define.cpp:7:22: error: expected ‘)’ before ‘;’ token
test-define.cpp:7:24: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
test-define.cpp:7:24: note: (if you use ‘-fpermissive’ G++ will accept your code)
test-define.cpp:7:27: error: expected ‘;’ before ‘)’ token
这将向您展示宏是如何展开的(提示从文件末尾开始并向上展开)我建议将宏替换为常量:
gcc -E file.c > result.txt
最好尽可能避免使用宏。宏没有任何作用域。它们是全局文本替换。编译器从未看到它们,因此如果使用调试器,它将不知道它们。我可能忘记了不使用宏的其他原因。应该是
#define N 10
。额外的分号导致有两个编译器希望在for语句中使用分号。@Jordan多年来帮了我很多次,特别是当您包含一些文件时,这些文件会执行一些奇怪的#define
,比如#define result int
,并且您有一个名为resultVery true的局部变量,我应该这么说,您应该添加为什么会出现这种情况的原因T比#定义好
gcc -E file.c > result.txt
const int N = 10;