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

// 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;

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:

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.

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.

Oh. DAA. I feel like a retard. Thanks. Did not know about the whole static thing. That response was quick.

This article has been dead for over six months. Start a new discussion instead.