0

所以我写了一个使用 XOR 位运算符交换两个数字的宏。使用 XOR 门交换数字的算法是众所周知的(即 a ^= b;b ^= a;a ^= b;),但在调用宏时我不断收到语法错误。它说它期望某个地方有一个分号。

确切的错误:

Line 73: error C2146: syntax error : missing ';' before identifier 'a'

这是我的程序遇到问题的文件。

#define swapNumbers(x, y, z) z = x, x = y, y = z;
#define swapStrings(str1, str2, str3)  str3 = str1, str1 = str2, str2 = str3;
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;


#include <iostream>
#include "protocol.h"
#include <string>

void PrintMenu()
{
std::cout << "\n\nChapter 16 -- Learn By Doings " << std::endl;
std::cout << "\n1. Learn By Doing 16.4 " << std::endl;
std::cout << "2. Learn By Doing 16.5 " << std::endl;
std::cout << "3. Learn By Doing 16.6 " << std::endl;
std::cout << "4. Exit " << std::endl;
NewLine();
}

void GetMenuChoice(int &menuChoice)
{
std::cin >> menuChoice;
}

void ExecuteMenuChoice(int &menuChoice)
{
switch(menuChoice)
{
case 1:
    {
        //Learn By Doing 16.4

        //Swap two numbers
        int x = 10;
        int y = 20;
        int z = 30;

        std::cout << "\nBefore swapping x is " << x << " and y is " << y << std::endl;
        swapNumbers(x, y, z);
        std::cout << "After swapping x is " << x << " and y is " << y << std::endl; 
        NewLine();

        //Swap two cStrings
        char * firstName = "Magnus";
        char * lastName = "Carlsen";
        char * tempName = "GOAT";

        std::cout << "\nBefore swapping, first name is " << firstName << " and last name is " << lastName << std::endl;
        swapStrings(firstName, lastName, tempName);
        std::cout << "After swapping, first name is " << firstName << " and last name is " << lastName << std::endl;

    }
    break;
case 2:
    //Learn By Doing 16.5
    {

    int x = 0;

    //Check if number is power of two
    std::cout << "\nEnter number: " << std::endl;
    std::cin >> x;

    NewLine();
    std::cout << ChangeBoolToString(isPowerOfTwo(x));

    //Swap numbers using bitwise operations
    NewLine();
    int a = 666;
    int b = 777;

    std::cout << "\n\nBefore swapping a is " << a << " and b is " << b << std::endl;
    swapUsingXOR(a, b);
    std::cout << "After swapping a is " << a << " and b is " << b << std::endl;

    }
    break;
case 3:
    //Learn By Doing 16.6

    break;
case 4:
    //Exit
    NewLine();

    break;
default:
    std::cout << "Invalid input.  Please enter a number between 1 and 4. " << std::endl;

}
}

void NewLine()
{
std::cout << ' ' << std::endl;
}

bool isPowerOfTwo(int binaryNumber)
{
while(((binaryNumber & 1) == 0) && binaryNumber > 1)
    binaryNumber >>= 1;

if(binaryNumber == 1)
    return true;
else
    return false;
}

char * ChangeBoolToString(bool function)
{
if(function == true)
    return "Your number is a power of two! ";
else
    return "Your number is not a power of two. ";
}
4

3 回答 3

4

您的宏定义中有一个额外的空间:

#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                    ^
                    |--- get rid of this space

分号也不是必需的。您可能想要添加一些大括号或使用 do/while(0) 技巧使您的宏表现得更像简单的语句。

如果您是初学者(看起来您是),您可能想查看clang。在大多数情况下,它会产生比大多数其他编译器更有用的错误消息。例如,一个带有你的宏的测试程序会给出这些消息,这使得发生了什么非常清楚:

example.c:14:5: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
    ^~~~~~~~~~~~
example.c:5:23: note: expanded from macro 'swapUsingXOR'
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                      ^
example.c:14:5: error: expected ';' after expression
    swapUsingXOR(a, b);
    ^
example.c:5:29: note: expanded from macro 'swapUsingXOR'
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                            ^
example.c:14:18: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
                 ^
example.c:14:5: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
    ^~~~~~~~~~~~
example.c:5:26: note: expanded from macro 'swapUsingXOR'
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                         ^
example.c:14:21: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
                    ^
4 warnings and 1 error generated.
于 2013-02-22T22:48:46.103 回答
1

太可怕了。不要做。在宏中多次引用相同的参数是灾难的根源......告诉我你的代码对此做了什么:

int *f1(); // function that returns an int pointer.
int *f2(); // function that returns an int pointer.
int x = 666;

// Quick... how many times is each function called? And is that 
// what the programmer expects based on the regular semantics of
// of the language? Are you sure it's safe to call those functions
// multiple times? Will the result be what you think it's going to be?
swapUsingXOR(*f1(), *f2());

// And what happens here?
swapUsingXOR(x, x);
于 2013-02-22T22:57:35.963 回答
0

阅读#define更多...

#define正如你所知道的宏,但是你把分号“;” 在那里!请记住,当您使用时#define,基本上只是为预处理器添加查找和替换注释......好吧,替换swapNumbers(x, y, z)z = x, x = y, y = z;导致问题......它保留分号“;”,并出错......

例如,它可以将分号放在不应该放在的地方,并swapNumbers(x, y, z);在最后制作两个!

在这里阅读更多:)

于 2013-02-22T22:58:21.620 回答