#define导致“预期的主要表达式”错误 [英] #define causes an "expected primary-expression" error

查看:91
本文介绍了#define导致“预期的主要表达式”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define N 10;

int main()
{
    int x;

    for (int i=0; i<N; i++)
        x = i;

    return 0;
}

用g ++编译的结果:

Result of compiling this in g++:

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

但它会编译当我将第7行更改为时很好(int i = 0; i< 10; i ++)

But it compiles fine when I change line 7 to for (int i=0; i<10; i++).

为什么是这个,我该如何使用 #define 指令来完成所需的操作?

Why is this and how can I use the #define directive to accomplish what I want?

推荐答案

删除分号-会很好-替换中包含分号

Remove the semicolon - you will be good - the semicolon is included in the substitution

有时,让编译器仅运行预处理器很有用。使用gcc / g ++,您可以执行以下操作

Sometimes it is useful to get the compiler to run the preprocessor only. With gcc/g++ you can do something like

gcc -E file.c > result.txt

这将向您展示宏的扩展方式(提示从文件末尾开始,工作)

This will show you how the macro expanded (hint start at the end of the file and work up)

这篇关于#define导致“预期的主要表达式”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆