But it's giving me error
[Linking error] undefined reference to '(anonymous namespace) :: add(int,int)'
And my another doubt is can we write the header file without the #macros i.r #ifndef or #define ?
By the way, avoid defining globals, especially in headers. The reason is that you'll come into issues with "duplicate names" which is a whole other barrel of worms. Instead use locals, especially in this situation. Drop the int x, and int y definitions in the headers and define it like so:
1 2 3 4 5 6 7
void add(int a, int b)
int x = a+b;
int y = a-b;
cout << "x" << x << endl ;
cout << "y" << y << endl;
To answer your last question: You don't need the #ifdef around a header, but the advantage is that it avoids errors when you include the headerfile recursively by accident, or include the same header twice.
Thanks for the reply but the problem that I was trying to solve is "Create a header file with an unnamed namespace . Include the header file in two separate cpp files and show that an unnamed space is unique for each translation unit.
How do I solve this then if I do not use the unnamed namespace?