| | |
Class Error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 8
Reputation:
Solved Threads: 0
Hello,
I am having problems with reading from a .h file to .cpp file it gives me a strange error i have gone over this thing 1000 times and can not figure out whats wrong. Could someone please help me? I am using dev.c++ compiler. I should mention i am new at this class thing:cheesy:
ERROR
2 C:\Documents and Settings\MACHINE\Desktop\DAVID\w.cpp In file included from C:\Documents and Settings\MACHINE\Desktop\DAVID\w.cpp
THIS IS THE .H FILE
I am having problems with reading from a .h file to .cpp file it gives me a strange error i have gone over this thing 1000 times and can not figure out whats wrong. Could someone please help me? I am using dev.c++ compiler. I should mention i am new at this class thing:cheesy:
ERROR
2 C:\Documents and Settings\MACHINE\Desktop\DAVID\w.cpp In file included from C:\Documents and Settings\MACHINE\Desktop\DAVID\w.cpp
THIS IS THE .H FILE
C++ Syntax (Toggle Plain Text)
// FILE w.h #ifndef SHIP_STATE #define SHIP_STATE class ship_state { public: ship_state(double h, double v, double f); double height(); double velocity(); double fuel(); int landed(); ship_state& update(double rate); void print(ostream& os); double dt const; double gravity const; double engine_strength const; double safe_velocity const; char burn_key const; private: double height_; double velocity_; double fuel_; }; #endif THIS IS THE .CPP FILE #include "w.h" #include <iostream> using namespace std; ship_state::ship_state(double h, double v, double f) { height_ = h; velocity_ = v; fuel_ = f; } double ship_state::height() {return height_;} double ship_state::velocity(){return velocity_;} double ship_state::fuel() {return fuel_;} int ship_state::landed() { if (height_<=0) return 1; return 0; } void ship_state:print(ostream& os) { os<<"Velocity :"<<velocity_<<endl; os<<"Height :"<<height_<<endl; os<<"Fuel :"<<fuel_<<endl; } ship_state& ship_state::update(double rate) { double dh; double dv; if (fuel_<=0) { fuel_=0; rate=0; } dh=velocity_*dt; dv=engine_strength*rate-gravity; fuel_ -= (rate*dt); height_ += dh; velocity_+=dv; return *this; } void end_game(ship_state&s); double get_burn_rate(); void lander_loop(ship_state& s) { s.print(cout); if (s.landed()) end_game(s); else lander_loop(s.update(get_burn_rate())); } void end_game(ship_state& s) { double v=s.velocity(); cout<<"Final velocity: "<<v; if (v>=safe_velocity) cout<<"...good landing!\n"; else cout<<"...you crashed!\n"; } double get_burn_rate() { cout<<"Enter "<<burn_key<<" to burn fuel, any other to float :"; char ch; cin>>ch; if (ch==burn_key) return 1.0; else return 0.0; } double dt=1.0 const; double gravity=0.5 const; double engine_strength=1.0 const; double safe_velocity = -0.5 const; char burn_key='b'const;
Last edited by nayrb; Mar 4th, 2007 at 3:25 pm.
The way you are trying to incorporate the wrong concept of const variables in your program is causing all the problems.
If you declare a variable as const, you need to specify the const qualifier before the dataype, like this:
Since const variables can't be modified (alteast not using the same name) you need to specify an initial value for the variable the way I have done above. Also since initialization of non static members inside a class is not allowed in C++, so you have to make those variables static.
You could also create a seperate header file with the constants and include it whenever you need to use the values.
If you declare a variable as const, you need to specify the const qualifier before the dataype, like this:
c Syntax (Toggle Plain Text)
const int SIZE = 10 ; // you must specify the value
Since const variables can't be modified (alteast not using the same name) you need to specify an initial value for the variable the way I have done above. Also since initialization of non static members inside a class is not allowed in C++, so you have to make those variables static.
C++ Syntax (Toggle Plain Text)
static const double dt=1.0 ; static const double gravity=0.5 ; static const double engine_strength = 1.0 ; static const double safe_velocity = -0.5 ; static const char burn_key = 'b';
You could also create a seperate header file with the constants and include it whenever you need to use the values.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Class Error ..... Help please (C++)
- Error trapping while reading windows XP registry entries (Python)
- Issues using Math class (Java)
- C++ CString Class Help! (C++)
- Template class (C)
- 6 Line class -> Me pounding head into wall (C)
- '[Class] is not abstract..' error when i use 'implements' (Java)
- My CGI Script gives me this error...What am I missing? (Perl)
- timing class (Java)
Other Threads in the C++ Forum
- Previous Thread: C++ and the Linux API
- Next Thread: C++ program that checks if...
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






