944,126 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2150
  • C++ RSS
Mar 4th, 2007
0

Class Error

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nayrb is offline Offline
8 posts
since Mar 2007
Mar 4th, 2007
0

Re: Class Error

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.

C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 4th, 2007
0

Re: Class Error

Oh. DAA. I feel like a retard. Thanks. Did not know about the whole static thing. That response was quick.
Reputation Points: 12
Solved Threads: 0
Light Poster
torbecire is offline Offline
46 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ and the Linux API
Next Thread in C++ Forum Timeline: C++ program that checks if...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC