By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Why can't I do this ?

P: n/a
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++])

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Alfonzo Morra wrote:
I have some code as ff:

#define DEF_ONE 0.0112
#define DEF_TWO 0.25

typedef struct {
You're a grown-up now, stop using C constructs. Just write

struct myStruct {
struct otherStruct *other ;
double this ;
'this' is a keyword. You can't use it here. Use a different identifier.
Try 'self' if that's what you need.
double that ;
}myStruct ;
MyClass {
myStruct ms ;
public:
MyClass(){
&ms.other = NULL ;
Uh... You're taking address of 'ms.other' and then trying to assign to
it. The address is an r-value. You can't assign to an r-value. I think
the compiler barfs here, not below. You probably mean to write

ms.other = NULL;
ms.this = DEF_ONE ; //<- compiler barfs here
'this' is a keyword. It has a very particular meaning in C++. You need
to use a different identifier.
ms.that = DEF_TWO ; //compiler barfs here too ..
I don't think so. I think you've got a compiler that can't count lines
well.
};

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++])


Learn C++, don't guess at it. It's much more complicated than C and it
requires a bit of effort. Don't expect to breeze through it even if you
did something in C (especially many moons ago).

V
Jul 23 '05 #2

P: n/a


Victor Bazarov wrote:
Alfonzo Morra wrote:
I have some code as ff:

#define DEF_ONE 0.0112
#define DEF_TWO 0.25

typedef struct {

You're a grown-up now, stop using C constructs. Just write

struct myStruct {
struct otherStruct *other ;
double this ;

'this' is a keyword. You can't use it here. Use a different identifier.
Try 'self' if that's what you need.
double that ;
}myStruct ;
MyClass {
myStruct ms ;
public:
MyClass(){
&ms.other = NULL ;

Uh... You're taking address of 'ms.other' and then trying to assign to
it. The address is an r-value. You can't assign to an r-value. I think
the compiler barfs here, not below. You probably mean to write

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

'this' is a keyword. It has a very particular meaning in C++. You need
to use a different identifier.
ms.that = DEF_TWO ; //compiler barfs here too ..

I don't think so. I think you've got a compiler that can't count lines
well.
};

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++])

Learn C++, don't guess at it. It's much more complicated than C and it
requires a bit of effort. Don't expect to breeze through it even if you
did something in C (especially many moons ago).

V


Apologies, my bad. I made a couple of brain-dead mistakes earlier on in
the code (not to mention the code I posted here). I guess it's time for
a coffee break after 9 hours straight at the terminal ... :-)

Jul 23 '05 #3

P: n/a
"Alfonzo Morra" <sw***********@the.ring.com> wrote in message
news:da**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
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++])


Wrong. You can assign defined constants to a variable. There are many other
errors in your code masking the line ms.this = DEF_ONE; which is not the
error in your code.
Add a small, running program to your code and work on debugging it. (Such
as:)
#include <iostream>
using namespace std;

int main( )
{
MyClass x;
cout << x.ms.other << ":" << x.ms.thus << ":" << x.ms.that << endl;
return 0;
}
--
Gary

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.