简体   繁体   English

为什么这个交换宏不会编译?

[英]Why won't this swap macro compile?

The K&R 2nd ed answer book has the following solution for a variable argument swap macro K&R 2nd ed答案书为可变参数交换宏提供了以下解决方案

#define swap(t, x, y) { t _z; \   
                       _z = y; \ 
                       y = x; \  
                       x = _z; } 

Visual Studio Express is telling me that y and x must be constant values and that there is an "expected declaration" to the left of the right brace. Visual Studio Express告诉我y和x必须是常量值,并且右括号左边有一个“预期声明”。

Is this formatting out of date? 这种格式是否过时了?

Edit: 编辑:

The way that code is formatted won't let me compile for the errors listed above, but the following code seems to be fine: 代码格式化的方式不会让我编译上面列出的错误,但以下代码似乎没问题:

#define swap(t, x, y) { t _z; _z = y; y = x; x = _z; }

The problem is you have whitespace after the \\ line continuation characters. 问题是你在\\ line continuation字符后面有空格。 As such, the \\ is no longer treated as a line continuation, but just a non-whitespace character. 因此, \\不再被视为行继续,而只是非空白字符。 This means the following lines are not part of the macro definition, and the compiler tries to compile them as normal lines. 这意味着以下行不是宏定义的一部分,编译器会尝试将它们编译为普通行。 Getting rid of the whitespace after each \\ will fix the error. 在每个\\之后摆脱空白将修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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