Archived

This topic is now archived and is closed to further replies.

Tevong

float topspeed=-1.#QNAN0?!??!??!?!??!

Recommended Posts

Here''s a simple program i did while learning OOP. class CVehicle //no base classes { friend class CWeathersystem; private: unsigned int weight,capacity; float topspeed; public: void printdata(); unsigned int getweight(){return weight;} unsigned int getcapacity(){return capacity;} float gettopspeed(){return topspeed;} int setweight(unsigned int newweight); int setcapacity(unsigned int newcapacity); int settopspeed(float newtopspeed); }; this is the Vehicle class. CWeathersystem is a friend class that has a apply() function to apply weather to the Vehicle data (no real use just to learn OOP..) class CWeathersystem { private: float windspeed,rainrate,sunlight; public: int apply(CVehicle* pVehicle); int setdefaults(float windspeed=10.0,float rainrate=25.0,float sunlight=1.21); }; now the function int apply(CVehicle* pVehicle) applies the modifications to CVehicle class data: int CWeathersystem::apply(CVehicle* pVehicle) { //check pointer is not null pointer if(!pVehicle) return 0; //modify CVehicle''s data using our private data pVehicle->topspeed/=((1-rainrate)*(20-windspeed)); return 1; }; so this is the problem: No matter what the values are at, apply() ALWAYS gives me a float topspeed result of -1.#QNAN0 ?!? Doesn''t make any sense. And also when I try to create an instance of CWeathersystem it gives me an error: int CWeathersystem::apply(CVehicle* pVehicle) { //check pointer is not null pointer if(!pVehicle) return 0; //modify CVehicle''s data using our private data pVehicle->topspeed/=((1-rainrate)*(20-windspeed)); return 1; }local_weather; //doesn''t work Thanks for taking your time to reply.. It is a bit long.

Share this post


Link to post
Share on other sites
First of all, it looks like you don''t have any constructors for your classes.
I''m surprised your code even compiles...

Share this post


Link to post
Share on other sites
I''m sure you didn''t mean to append local_weather to the end of the apply() function. If you did, you need to relearn your OOP (since you''re just learning, that''s cool)

To create an instance of an object you call a constructor, right?
CWeatherSystem local_weather 

If you haven''t provided a constructor, the compiler tries to provide one for you. This compiler-provided default constructor does absolutely no initialization, and you cannot call methods of this object (since it isn''t instantiated, its data is garbage).

If you do provide a constructor, make sure you provide a default constructor (one that either take no arguments or has default values for all of its constructors) so you can declare uninitialized objects.

Also, the CWeathersystem class has no need for private access to CVehicle; it can set the vehicle''s top speed using settopspeed() and gettopspeed(). Don''t break encapsulation for convenience.

Share this post


Link to post
Share on other sites
yeah I know I could''ve used gettopspeed and stuff but I was just learning how to friend classes and stuff.

The book I read added local_weather at the end of apply... Next bit is about constructors and destructors which I haven''t got to yet.

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi

If you haven''t provided a constructor, the compiler tries to provide one for you. This compiler-provided default constructor does absolutely no initialization



Not true. Base class constructors and default constructors for class objects will be called.

quote:
you cannot call methods of this object (since it isn''t instantiated, its data is garbage).



And who is going to stop you? There are lots of cases where it would be perfectly valid to call methods on an object without a user-defined constructor.(mutators, classes without any data, etc... )



"A society without religion is like a crazed psychopath without a loaded .45"

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
Not true. Base class constructors and default constructors for class objects will be called.

What happens when you don''t provide a default constructor, and don''t inherit from any base class?

Compile error, where you try to declare an object instance? I don''t know what the standard says on this, and I''m not a language specialist (yet), so I''d love for you to let me know. Thanks.

quote:
And who is going to stop you? There are lots of cases where it would be perfectly valid to call methods on an object without a user-defined constructor.(mutators, classes without any data, etc... )

In the specific instance where the compiler belches on your object declaration, this would probably have resulted in invalid operations (which is why I assume the compiler actually belched). Sure, there are lots of object types where you don''t call a constructor (all those examples you referred to, COM interfaces, etc).

I''d appreciate it if you''d clarify this for me (I find a copy of the ''98 standard where I am). Thanks.

Share this post


Link to post
Share on other sites
If you declare a pointer to a class, but dont initialize it, or instantiate it, you can still call it''s member functions, as long as they dont access any of the class''s data. If they do, Access Violation.

Creating classes without Constructors is perfectly legal. You just dont get any initialization. It''s kinda like "CClass* Instance = malloc(sizeof(CClass));". No initialization, but the object is instantiated anyway.

Z.

Share this post


Link to post
Share on other sites
Oluseyi: I''m actually not sure what you are asking. When you declare an object on the stack, user defined ctor or no user defined ctor, memory will be allocated for the object. You can call whatever methods you like on the object, even methods that access member data(oc for primitives, the data will be uninitialized and just contain garbage. Member objects(not pointers) will have their default(user-defined or compiler-supplied) ctors called).
The same goes for an object that is allocated on the heap with new(malloc does not call constructors).
If you have a pointer to an object, and you havent new''ed it, calling methods on the pointer which access member data will most likely cause an access violation. If the method does not access member data, the method will complete without problems.

  
class Blargh
{
int x;
public:
void setX( int x ) { this.x = x; }
int getX() const{ return x; }
};

//This will compile just fine.

//myBlargh is allocated on the stack,

//with x being garbage.

Blargh myBlargh;

int bleh = myBlargh.getX(); //This obviously returns garbage


myBlargh.setX( 42 ); //no problem here



quote:

What happens when you don''t provide a default constructor, and don''t inherit from any base class?


There is always a default constructor. Either you write it, or the compiler supplies it. I was anyway talking about member objects, as in class Blagh{ Blegh myBlegh; }; The default ctor for myBlegh will be called.


"A society without religion is like a crazed psychopath without a loaded .45"

Share this post


Link to post
Share on other sites
Making a constructor fixed my problem, thanks. It now returns a proper number.

Now I''m trying to redo my basic tictactoe game written when I was learning C. I''m trying to redo it with OOP, a table class with functions to modify the tictactoe table, draw it etc...

Here''s my endgame function (not part of table class):

void endgame(char* pTable,int endtype)
{
clrscreen();
pTable->drawtable(); //problem is here
switch(endtype)
{
case 1:
cout<<"\n\nCongratulations, You win!";
break;
case 2:
cout<<"\n\nYou lose! Try again.";
break;
default:
cout<<"\n\nDraw!";
break;
}

while(1){}
return;
}

pTable->drawtable gives me an error that says:

request for member ''drawtable'' in ''*pTable'', which is of non-aggregate type ''char''

Any idea what that means?

Share this post


Link to post
Share on other sites
Look at this:

void endgame(char* pTable,int endtype)

You are declaring pTable to be a pointer to a character, and since a character has no method "drawtable" the compiler complains. You should change that function-declaration to something like:

void endgame(CTable *pTable,int endtype)

(if your table class is called CTable).

Share this post


Link to post
Share on other sites