error # is not followed by a macro parameter

How can I get past this errer "# is not followed by a macro perimeter on line:
#define ASSERT(x) \

I copied this straight out of my book can anyone show me how I can get it to work thanks.

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
    #define DEBUG

#include<iostream>

#ifndef DEBUG
    #define ASSERT(x)
#else
    #define ASSERT(x) \
#if(! (x)) \
{
    std::cout <<"Error! Assert " << #x << " falied \n"; \
    std::cout << " on line " <<__LINE__<< "\n"; \
    std::cout << " in file " <<__FILE__<< "\n" \
}
#endif

int main()
{
    int x =5;
    std::cout<<"First assert: \n";
    ASSERT (x==5);
    std::cout<<"\nSecond assert: \n";
    ASSERT (x !=5);
    std::cout <<"\nDone.\n";
} \
I looked on stack exchange and it said I can't use ifndef in that order so I changed it to this(see below) but then my errror messages dont fire/ Whats going on, I want to be able to check the x condition with the assert and get the error messages . Please help thanks.

#ifndef DEBUG
#define ASSERT(x)

#define DEBUG

#include<iostream>


#else
#define ASSERT(x) \
#if(! (x)) \
{
std::cout <<"Error! Assert " << #x << " falied \n"; \
std::cout << " on line " <<__LINE__<< "\n"; \
std::cout << " in file " <<__FILE__<< "\n" \
}
#endif

int main()
{
int x =6;
std::cout<<"First assert: \n";
ASSERT (x==5);
std::cout<<"\nSecond assert: \n";
ASSERT (x !=5);
std::cout <<"\nDone.\n";
}
The #if(!(x)) needs to be a regular if, not a preprocessor if. And you need to make sure you have a backslash at the end of every line you want in the macro (except the last).
what is wrong with this why are the error messages not firing

#ifndef DEBUG
#define ASSERT(x)

#define DEBUG

#include<iostream>


#else
#define ASSERT(x) \
if(! (x)) \
{
std::cout <<"Error! Assert " << #x << " falied \n"; \
std::cout << " on line " <<__LINE__<< "\n"; \
std::cout << " in file " <<__FILE__<< "\n" \
}
#endif

int main()
{
int x =5;
std::cout<<"First assert: \n";
ASSERT (x==5);
std::cout<<"\nSecond assert: \n";
ASSERT (x !=5);
std::cout <<"\nDone.\n";
}
The code you've posted doesn't even compile (and you didn't even bother to post it in code tags).
hi this does compile on my system but the error messages don't appear. please help me make the error messages appear. Sorry for posting without the tags before thanks.

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>

#ifndef DEBUG
#define ASSERT(x)

#define DEBUG

#else
#define ASSERT(x) \
if(! (x)) \
{
    std::cout <<"Error! Assert " << #x << " falied \n"; \
    std::cout << " on line " <<__LINE__<< "\n"; \
    std::cout << " in file " <<__FILE__<< "\n" \
} 
#endif

int main()
{
    int x =5;
    std::cout<<"First assert: \n";
    ASSERT (x==5);
    std::cout<<"\nSecond assert: \n";
    ASSERT (x !=5);
    std::cout <<"\nDone.\n";
}
How can it compile if it's missing the backslash at the end of line 11? And the semicolon before the backslash on line 14?

Do you know what "compile" means?

Also, you defined DEBUG in the wrong place. Think, man!
Last edited on
Hi got it working now many thanks for your helpful advice. Sorry for my sytax errors. Thanks.
You didn't answer tpb's question:

tpb wrote:
Do you know what "compile" means?
Topic archived. No new replies allowed.