syntax for C++ initializer list?
typedef struct Point3D
{
double x; // latitude position
double y; // longitude position
double z; // altitude position
} Point3D;
class Body
{
public:
Body(); // default consructor takes no arguments
~Body();
Point3D position;
double heading;
Body::Body()
: position.x(0.0),
position.y(0.0),
position.z(0.0),
heading(0.0)
{
Keeps bombing out at the : position.x(0.0)
I've tried position->x(0.0), x(0.0) and other clueless stabs in
the dark.
I assume this is trivial but please, humilate me in front of the world
{
double x; // latitude position
double y; // longitude position
double z; // altitude position
} Point3D;
class Body
{
public:
Body(); // default consructor takes no arguments
~Body();
Point3D position;
double heading;
Body::Body()
: position.x(0.0),
position.y(0.0),
position.z(0.0),
heading(0.0)
{
Keeps bombing out at the : position.x(0.0)
I've tried position->x(0.0), x(0.0) and other clueless stabs in
the dark.
I assume this is trivial but please, humilate me in front of the world
I think that C++ has not yet created space for the Point3D structure. The solution to this is to not use the initializer list here. Changing to position->(0.0) would do nothing since position is not a pointer.
This should work though...
This should work though...
Code:
Body::Body(): heading(0.0)
{
position.x(0.0);
position.y(0.0);
position.z(0.0);
}
You would need to give Point3D a constructor in order to be able to initialize it in the initializer list I think. The only thing that might possibly work is
Code:
Body::Body() : position({0.0, 0.0, 0.0}) {}
I'm almost certain (without trying it) that you can't use BlackTiger's 'position.x(0.0);' notation. You'd need to assign the value using 'position.x = 0.0;' instead.
I'd agree with OSC's suggestion that Point3D should have a constructor like this:
That would allow Body's constructor to be:
You might also want to have a default constructor like this:
Then you wouldn't need to mention points which should be zero in your initialiser list at all.
One last thing - there's no need to use C-style 'typedef struct Name {...} Name;' in C++. You should write this instead:
I'd agree with OSC's suggestion that Point3D should have a constructor like this:
Code:
Point3D(double inX, double inY, double inZ) : x(inX), y(inY), z(inZ) {}That would allow Body's constructor to be:
Code:
Body() : position(0.0, 0.0, 0.0), heading(0.0) {}You might also want to have a default constructor like this:
Code:
Point3D() : x(0.0), y(0.0), z(0.0) {}Then you wouldn't need to mention points which should be zero in your initialiser list at all.
One last thing - there's no need to use C-style 'typedef struct Name {...} Name;' in C++. You should write this instead:
Code:
struct Point3D
{
// Constructors here if you like
double x; // latitude position
double y; // longitude position
double z; // altitude position
};NCarter Wrote:Code:
Point3D() : x(0.0), y(0.0), z(0.0) {}
Best would just be to have 1 Point constructor with default arguments:
Code:
Point3D(float inX = 0.0, float inY = 0.0, float inZ = 0.0)
: x(inX), y(inY), z(inZ)
{ }
[quote]struct Point3D
{
// Constructors here if you like
double x; // latitude position
double y; // longitude position
double z; // altitude position
};[/QUOTE
Thanks all. I now see that position is not a "primitive" type and so needs a constructor.
However, does this mean that Point3D needs to be a class? Or does the above
pattern imply that a constructor can be within just a C++ struct? I hope so, because
that would be pretty sweet.
{
// Constructors here if you like
double x; // latitude position
double y; // longitude position
double z; // altitude position
};[/QUOTE
Thanks all. I now see that position is not a "primitive" type and so needs a constructor.
However, does this mean that Point3D needs to be a class? Or does the above
pattern imply that a constructor can be within just a C++ struct? I hope so, because
that would be pretty sweet.
WhatMeWorry Wrote:However, does this mean that Point3D needs to be a class? Or does the above pattern imply that a constructor can be within just a C++ struct?It can be a class or a struct, whichever you prefer. The two things are equivalent in every way in C++, except that struct members are public by default and class members are private by default. You can therefore have constructors, methods and anything else you like in a C++ struct.
I'd probably make Point3D a class, but it's entirely up to you.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| "Initializer element is not constant" for NSNumbers | Coyote | 5 | 4,649 |
Oct 22, 2009 09:37 AM Last Post: OneSadCookie |
|
| Syntax error | BinarySpike | 5 | 2,845 |
Aug 21, 2005 04:24 PM Last Post: BinarySpike |
|

