There is a question which make me puzzle. I know it is not right to do that ,but I don't know why. And how does the #ifndef #define #endif work.How the compliers deal with the varialbles like the following "a" and "b" ;
The code is simple:(myh.h):
#ifndef P_H
#define P_H
struct P{
int a;
};
int b;
#endif
another file s.cpp
#include"myh.h"
P a1;
the main.cpp:
#include<iostream>
#include"myh.h"
using namespace std;
int main()
{
P a2;
return 0;
}
The error is multiple definition of b; just as I know. I have two questions: 1.as some books said if you use #ifndef the compliers will not include it twice , then why the "b" seem be included twice .
- What is the differnce of "a" 、 "b"and "P". Why "a" and "P" have no question . I don't know wether I am right to consider "P" as a variable the same level with "b"? Or "P" is just a definition of type.
If It is the difference of local and global ,why is "P" right?
I am really puzzled. Pardern your time to help me. Thanks.