🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

help with class constructors

Started by
4 comments, last by tryandgetme 22 years, 9 months ago
hi all. I wrote a class for a simple game I'm making, and I'm getting stuck with the constructor. I've never had problems with classes before cause I've never needed constructors. I wrote the class and constructor with the tutorial right beside me, and as far as I can see from the tutorial, it should work. the compiler is giving me the error: chopper.cc:34: return type specification for constructor invalid here's the class and it's constructor class chopper { public: chopper(); ~chopper(); private: int angle; int turnrate; float x, y, currentspeed, maxspeed, accel ,drag; void userinput(); void animate(); }; chopper::chopper() { /*this is the line it's complaining about*/ angle = 0; x = 320; y = 200; maxspeed = 10; currentspeed = 0; accel = 0.25; turnrate = 4; drag = 0.1; } thanks for any help you can give btw, is this the best place to post these type of questions, or should I go somewhere else, like general? Edited by - tryandgetme on September 5, 2001 2:03:30 PM
Advertisement
Your class compiles without any errors with VC++. It only gives a warning that the value assigned to drag is converted from const double to float which can be avoided by writing an "f" behind the number (drag = 0.1f.
I hope you also defined the destructor of the chopper class. When I created an instance of class chopper it complained that the destructor isn''t defined. (needs something like chopper::~chopper() { /*destroy object*/ })
Which compiler are you using? VC++ needs the extension .cpp for c++ source files.Maybe you just have to recompile everything.

baumep
baumep
I'm using the DJGPP compiler for dos (www.djgpp.org). it did everything just fine until I added the constructor and destructor. maybe I should stop calling it names hmm...I guess I'll just keep working at it and see what I come up with. anyone out there use the DJGPP compiler and have this problem?
and, if no one can help me with that, can you help me understand the error message better?

Edited by - tryandgetme on September 5, 2001 6:47:09 PM
ok, I did some fiddling around, tried devc++ (www.bloodshed.net) , djgpp, and the compiler that comes with Redhat linux 7, and ms visual c++. the error only happens when using linux, devc++ or djgpp. devc++ and linux both use the gcc compiler, and devc++ uses mingw. does this make any sense to anyone?
This is against all rules of c++ and may sound crazy but probably your compiler expects a void return type to the definition of the constructor.
Try to implement your constructor as void chopper::chopper() {...} and see if it results in an error (it should). Or try putting a void before the declaration of the constructor.

baumep
baumep
As it turns out, I did make my class right, but made a typeing error that took me a week to catch. everything works great now. Thanks for your help.

This topic is closed to new replies.

Advertisement