Class Error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 8
Reputation: nayrb is an unknown quantity at this point 
Solved Threads: 0
nayrb nayrb is offline Offline
Newbie Poster

Class Error

 
0
  #1
Mar 4th, 2007
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

  1.  
  2. // FILE w.h
  3.  
  4.  
  5. #ifndef SHIP_STATE
  6. #define SHIP_STATE
  7.  
  8. class ship_state
  9. {
  10. public:
  11.  
  12. ship_state(double h, double v, double f);
  13. double height();
  14. double velocity();
  15. double fuel();
  16. int landed();
  17. ship_state& update(double rate);
  18. void print(ostream& os);
  19. double dt const;
  20. double gravity const;
  21. double engine_strength const;
  22. double safe_velocity const;
  23. char burn_key const;
  24. private:
  25. double height_;
  26. double velocity_;
  27. double fuel_;
  28. };
  29.  
  30. #endif
  31.  
  32. THIS IS THE .CPP FILE
  33.  
  34.  
  35. #include "w.h"
  36. #include <iostream>
  37. using namespace std;
  38.  
  39. ship_state::ship_state(double h, double v, double f)
  40. {
  41. height_ = h;
  42. velocity_ = v;
  43. fuel_ = f;
  44. }
  45.  
  46. double ship_state::height() {return height_;}
  47. double ship_state::velocity(){return velocity_;}
  48. double ship_state::fuel() {return fuel_;}
  49.  
  50. int ship_state::landed()
  51. {
  52. if (height_<=0)
  53. return 1;
  54. return 0;
  55. }
  56.  
  57. void ship_state:print(ostream& os)
  58. {
  59. os<<"Velocity :"<<velocity_<<endl;
  60. os<<"Height :"<<height_<<endl;
  61. os<<"Fuel :"<<fuel_<<endl;
  62. }
  63.  
  64. ship_state& ship_state::update(double rate)
  65. {
  66. double dh;
  67. double dv;
  68. if (fuel_<=0) {
  69. fuel_=0;
  70. rate=0;
  71. }
  72. dh=velocity_*dt;
  73. dv=engine_strength*rate-gravity;
  74. fuel_ -= (rate*dt);
  75. height_ += dh;
  76. velocity_+=dv;
  77. return *this;
  78. }
  79. void end_game(ship_state&s);
  80. double get_burn_rate();
  81.  
  82. void lander_loop(ship_state& s)
  83. {
  84. s.print(cout);
  85. if (s.landed())
  86. end_game(s);
  87. else
  88. lander_loop(s.update(get_burn_rate()));
  89. }
  90.  
  91. void end_game(ship_state& s)
  92. {
  93. double v=s.velocity();
  94. cout<<"Final velocity: "<<v;
  95. if (v>=safe_velocity)
  96. cout<<"...good landing!\n";
  97. else
  98. cout<<"...you crashed!\n";
  99. }
  100.  
  101. double get_burn_rate()
  102. {
  103. cout<<"Enter "<<burn_key<<" to burn fuel, any other to float :";
  104. char ch;
  105. cin>>ch;
  106. if (ch==burn_key)
  107. return 1.0;
  108. else
  109. return 0.0;
  110. }
  111. double dt=1.0 const;
  112. double gravity=0.5 const;
  113. double engine_strength=1.0 const;
  114. double safe_velocity = -0.5 const;
  115. char burn_key='b'const;
Last edited by nayrb; Mar 4th, 2007 at 3:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Class Error

 
0
  #2
Mar 4th, 2007
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:

  1. 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.

  1. static const double dt=1.0 ;
  2. static const double gravity=0.5 ;
  3. static const double engine_strength = 1.0 ;
  4. static const double safe_velocity = -0.5 ;
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 46
Reputation: torbecire is an unknown quantity at this point 
Solved Threads: 0
torbecire torbecire is offline Offline
Light Poster

Re: Class Error

 
0
  #3
Mar 4th, 2007
Oh. DAA. I feel like a retard. Thanks. Did not know about the whole static thing. That response was quick.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC