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

Victor Bazarov

Alfonzo said:
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
 
A

Alfonzo Morra

Victor said:
Alfonzo said:
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 ... :)
 
G

Gary Labowitz

Alfonzo Morra 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 ;
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;
}
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top