Why can't I do this ?

A

Alfonzo Morra

I have some code as ff:

#define DEF_ONE 0.0112
#define DEF_TWO 0.25

typedef struct {
struct otherStruct *other ;
double this ;
double that ;
}myStruct ;


MyClass {
myStruct ms ;
public:
MyClass(){
&ms.other = NULL ;
ms.this = DEF_ONE ; //<- compiler barfs here
ms.that = DEF_TWO ; //<- compiler barfs here too ..
}
....
};

Is it not possible to assign #defines to variables (I'm sure I've done
this several times using C - many moons ago [granted C is not C++])
 
R

Rolf Magnus

Alfonzo said:
I have some code as ff:

#define DEF_ONE 0.0112
#define DEF_TWO 0.25

typedef struct {
struct otherStruct *other ;
double this ;
double that ;
}myStruct ;


MyClass {
myStruct ms ;
public:
MyClass(){
&ms.other = NULL ;

What's that supposed to do? This should also generate an error message. You
probably meant:

ms.other = NULL ;
ms.this = DEF_ONE ; //<- compiler barfs here

'this' is a keyword.
ms.that = DEF_TWO ; //<- compiler barfs here too ..

This line seems fine to me. What's the compiler's error message?
}
....
};

Is it not possible to assign #defines to variables (I'm sure I've done
this several times using C - many moons ago [granted C is not C++])

#define is just like a copy/paste functionality built into the compiler. If
you write DEF_TWO in your code somewhere, the preprocessor replaces it with
0.25 before the actual compiling starts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top