使用按位运算符的宏语法错误

时间:2013-02-22 22:45:33

标签: c++ macros bit-manipulation

所以我编写了一个使用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. ";
}

3 个答案:

答案 0 :(得分: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.

答案 1 :(得分:1)

这很平常 HORRIBLE 。不要做。多次在宏中引用相同的参数是一个灾难的秘诀...告诉我你的代码对此做了什么:

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);

答案 2 :(得分:0)

了解#define更多...

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

例如,它可以将分号放在它们不应该的位置, 和swapNumbers(x, y, z);最后两个!

阅读更多here:)