Initializing a variable in the ctor
"cWorld.h"
(...)
private:
cObject worldObj();
cObject *ptr = &worldObj;
cLinkedList<cObject> world(ptr);
That's what I'd like to do! Or:
"cWorld.cpp"
cWorld::cWorld(void)
{
cObject worldObj;
cObject *ptr = &worldObj;
cLinkedList<cObject> world(ptr);
}
(...)
Or:
"cWorld.h"
private:
cObject worldObj();
cObject *ptr;
"cWorld.cpp"
cWorld::cWorld(void)
{
ptr = &worldObj;
cLinkedList<cObject> world(ptr);
}
As you can see, I need a cLinkedList class, type cObject, named world and it *must* be initialized with a pointer to the type of class/variable that cLinkedList contains.
The first way complains that I can't set ptr in the definition. (error C2864: 'cWorld:
tr' : only static const integral data members can be initialized within a class)
The second way doesn't allow me to use world in the functions in cWorld.cpp. (error C2065: 'world' : undeclared identifier)
The third way says: (error C2276: '&' : illegal operation on bound member function expression)
cLinkedList works as it should. It has been testdriven with the string class and some other types of variables.
My question is: How do I make this world object that I need, and what does the third error mean?
Thanks in advance,
Nick
EDIT:
Also tried this:
private:
double precision;
cObject worldObj;
cLinkedList<cObject> world(&worldObj);
Since that seemed the solution (can't initialize a variable, ptr, in the class), but that gives me: (error C2059: syntax error : '&').
Which I really don't understand, because that should give me the adress of worldObj. Why doesn't that work?
[1897 byte] By [
Clockowl] at [2008-1-10]
You seem to be confused between declaring an data member and initializing the data member (might I suggest a C++ refresher course?).
In C++ you can only 'inline' initialize a very restricted set of data-members - only static const integral members - this explains C2864. So you should not be attempting to 'inline' initialize any of the data member in your example.
If I go back to your first example it seems that you have three data members:
Code Snippet
private:
cObject worldObj();
cObject *ptr = &worldObj;
cLinkedList world(ptr);
These should be actually declared as follows:
Code Snippet
private:
cObject worldObj;
cObject *ptr;
cLinkedList world;
You should then initialize these data members in each constructor:
Code Snippet
cWorld::cWorld()
: worldObj(), ptr(&worldObj), world(ptr)
{
}
Note: you don't need to initialize worldObj as it the compiler will automatically default initialize it for you so you could reduce the code to:
Code Snippet
cWorld::cWorld()
: ptr(&worldObj), world(ptr)
{
}
Hey, back again,
It's more or less the same question:
Why can't I do this?
Code Snippet
cObject::cObject(real x, real y, real z, real vx, real vy, real vz, real ax, real ay, real az, real mass)
: postision.x(x), position.y(y), position.z(z), speed.x(vx), speed.y(vy), speed.z(vz), accel.x(ax), accel.y(ay), accel.z(az)
{
if
(!mass) cerr << "Mass was zero! Correcting..." << endl;mass = (mass) ? mass : (real)
0.00000001;invMass =
1/mass;}
error C2059: syntax error : '.'
But I need to set those member variables! Do I need to do it in the code or is there some other way around?
Thanks again,
In a constructor initialize list you can only initialize direct members of the current class. So an expression like:
Code Snippet
position.x(x)
makes no sense here. If position, speed and accel are members of cObject then you need to forward the appropriate values on to their constructors. Something like:
Code Snippet
cObject::cObject(real x, real y, real z, real vx, real vy, real vz, real ax, real ay, real az, real mass)
: positsion(x, y, z), speed(vx, vy, vz), accel(ax, ay, az)
Or pass in already constructed objects:
Code Snippet
cObject::cObject(const Position& position, const Speed& speed, const Accel& accel, real mass)
: positsion_(position), speed_(speed), accel_(accel)
Also see my earlier comments about taking the time to learn the basics of C++.
I've seen them, and I got the book, it's a nice reference, but I don't want to start reading about cout again >_>
Anyway, thanks again. I'm pretty sure I read all this but I just forgot it, sorry for posting "dumb" questions here, but I appreciate the help a lot!
Anyway, ran into a really odd (runtime) problem now, but that doesn't fit in this thread.