This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Namespaces and pre-processing directives


At 15.49 04/04/2003 +0530, Megha Murarka wrote:
Hi All,

The program given below compiles fine.
but if we use the statement ,
int x = MYSPACE::VALUE +1;
 It gives an error. Why is it so ? (when the #define is present inside the
scope of the namespace ) ????
****************************************************************************
**
namespace MYSPACE{
    #define VALUE 50;
};
int main(){
    int x = VALUE + 1;
    return 1;
}

Regards,
Megha

#define's are handled by the preprocessor, and like macros are unaffected by namespaces, classes, etc.
If you try to use them, the compilersees:


namespace MYSPACE{
};
int main(){
    int x = MYSPACE::50 + 1;
    return 1;
}

which has not much sense...


If you need to use namespaces for constants, use enumes instead:

namespace MYSPACE{
    enum {
        VALUE = 50
    };
};
int main(){
    int x = MYSPACE::VALUE + 1;
    return 1;
}

works.

fwyzard


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]