Creating macros and errors encountered.

Good Day.

I am using a program to verify certain results and I would like to replace my function calls with macros to speed up the verification process.

However, my initial attempts to replace simple functions with macros have always resulted in compiler errors.

The macro, mMul, in the attached code has to be designed to multiply two hexadecimal numbers. Since the code for multiplying has more procedures in it, I have shortened it to just XOR two hexadecimal numbers.

But compiling this code always gives the compiler error

In function 'int main()'::
expected primary-expression before '{' token
expected ';' before '{' token

at the line "cout << mMul(a, b);"

I believe my code is syntactically correct. But I just do no understand what this error is telling me.

Any help/ advice with this code and on how to design a proper macro will be greatly appreciated.

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream> // for cin and cout
#include <stdint.h> // to define uint8_t

using namespace std;

#define mMul(a,b)   \
{                   \
    uint8_t prod = 0x00;\
    prod = a^b;         \
}

int main()
{
    int a, b;
    
    cin >> hex >> a;
    cin >> hex >> b;
    
    cout << mMul(a, b);
    
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}
Would you write: cout << { uint8_t prod = 0x00; prod = a^b; };?

That is what your macro expands to. I don't see how using macros would speed up the verification process.
Last edited on
Thank you!

I get what my mistake is now.

The main program in which I need to use macros instead of function calls has more functions than this and I believe it will only help speed up the process by a small amount.
better use inline instead.
Topic archived. No new replies allowed.