I wrote a program with classes. When in one file it works fine. But when i split them up into three the main program won't read the other two WHY?

.H FILE

// FILE w.h


#ifndef LUNAR_LANDER
#define LUNAR_LANDER

class lunar_lander
{
public:
       
   lunar_lander(float);//, float, float, float, float, float, float);
   
    // ALTITUDE AND ACCESSOR
   float alt(float, float, float);
   void setalt(float alt);
   const float getalt();

   // VERTICAL SPEED AND ACCESSOR
   float vvel(float, float, float);
   
   // FUEL IN TANK AND ACCESSOR
   float tankf();
   
   // FUEL FLOW RATE AND ACCESSOR
   float frate(float);
   void frate();
        
   // LANDER MASS AND ACCESSOR
   float massl();
   
   // LANDER THRUST AND ACCESSOR
   float thrust();

   // MAX FUEL CONSUMPTION RATE AND ACCESSOR
   float fuelconsum(float , float , float );
   
   // WHEN LUNAR LANDS
   int landed();
     
   // TIME
   float time(float);
   void time();
   
private:
        
   float alt_;
   int count;
   float vvel_;
   float tankf_;
   float frate_;
   float fuelconsum_;
   float thrust_;
   float massl_;
   float time_;
};

#endif

IMPLEMENTATION FILE

#include "56.h"
#include <iostream>
#include <climits>
using namespace std;   
   
lunar_lander::lunar_lander(float a) //float fl, float vs, float t, 
//float fr, float thr, float ml)
{
   alt_ = a;
  // vvel_ = vs; 
  // fuelconsum_ = fl;
  // time_ = t ;
  // frate_ = fr;
  // thrust_ = thr;
   //massl_ = ml;
   
   //fr = t* frate_ * fl;
}
// ALTITUDE OF THE LUNAR LANDER
float lunar_lander::alt(float a, float vs, float t)
{
      alt_ = a;
      vvel_ = vs; 
      time_=t;
      a = t * vs;
      return a;
       }
// VERTICAL VELOCITY
float lunar_lander::vvel(float t, float ml, float thr)
{
      time_ = t;
      thrust_ = thr;
      massl_ = ml;
       return    t * ( thr/ml - 1.62); 
       }
// FUEL CONSUMPTION
float lunar_lander::fuelconsum( float t, float fr, float ml) 
{
       time_ = t;
      fuelconsum_ = fr;
      massl_ = ml;
      t * fr * ml;
       return t * fr * ml;;
       }
// MASS OF THE LUNAR_LANDER W/0 FUEL
float lunar_lander::massl() 
{
      return massl_;
      }
// INTIAL SPEED OF THE LUNAR_LANDER      
float lunar_lander::thrust() 
{
      return thrust_;
      }
      
// CORRECTS FRATE IF TANKS IS EMPTY
float lunar_lander::frate(float tankf)
{
      if(tankf = 0)
      {
                    frate_ = 0;
                    }
      return frate_;
      }
// CHECKS IF TANK IS FULL OF FUEL
float lunar_lander::tankf()
{
      return tankf_;
      }  
// TELLS IF THE LANDER HAS LANDED
int lunar_lander::landed()
{
   if (alt_<=0)
      return 1;
   return 0;
}
// CORRECTS IF TIME GOES BELOW ZERO
float lunar_lander::time(float q)
{
    time_ = 0.0;
    time_ ++;
    if (time_ <= 0.0)
    {
             time_ = 0.0;
             }
}

// FUEL RATE
void lunar_lander::frate()
{
   for(float i = 0.0; i < 1.0; ++i)
     frate_++;
     }
     
// MUTATOR THAT ALLOWS TIME TO PASS
void lunar_lander::time()
{
     for(float z = 0.0; z < INT_MAX; ++z)
     time_++;
     }
const float lunar_lander::getalt()
{
     return alt_;
     }
void lunar_lander::setalt(float alt)
{
     if(alt >0 && alt <INT_MAX)
     {
            alt = alt_;
            }
     else
     cerr << "What" << endl;
     }
   float thrust = 5000;
   float alt = 1000;
   float frate = 0;
   float vvel_ = 0;
   float tankf = 1700;
   const float massl = 900;
   const float fuelconsum = 10;
   const float gravity = 1.62;

MAIN PROGRAM

#include "56.h"
#include "56.cpp"
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    lunar_lander y(14);
    y.setalt(15);
    cout << y.getalt() << endl;
    getch();
}

THE MAIN won't read from the first two, but the implemention file will read from the 56.h.?

Dani AI

Generated

is right: implementation (.cpp) files must not be #included into other translation units. Headers declare interfaces, .cpp files define them and each .cpp should be compiled separately and then linked together. 's advice about adding all source files to the IDE project is the usual fix in an environment like Dev-C++.

Two safe workflows:

  • IDE: create a project and add both .cpp files to it (Project → Add source/add existing files, varies by IDE). Do not #include the .cpp file from main; leave only the header included.
  • Command line: compile each .cpp to an object and link. Example commands:
    g++ -Wall -Wextra -c 56.cpp
    g++ -Wall -Wextra -c main.cpp
    g++ 56.o main.o -o lunar_lander

Common linker/compiler symptoms and what they mean:

  • “multiple definition” errors — caused by including a .cpp into more than one translation unit or compiling the same definitions twice.
  • “undefined reference” errors — the .cpp that defines those symbols was not compiled/linked.
  • Lots of warnings or strange runtime behavior — mismatched declarations/definitions, non-void functions missing returns, accidental use of assignment in conditionals, or stray globals at file scope.

Practical checks to perform: confirm every function signature in the .cpp exactly matches the header (parameters and const qualifiers); remove any accidental global definitions at the end of the implementation file; replace assignments-in-conditions with comparisons; ensure non-void functions return a value; and enable high warning levels (for g++ use -Wall -Wextra) to catch these problems early.

Recommended Answers

All 3 Replies

never ever include one *.cpp file inside another one. #include's are only for header files with *.h extension and often with no extension at all. If your program consists of two or more *.cpp files then each file should be compiled as a distinct unit then finally all object files and libraries are linked together. How exactly to do that depends on the compiler you are using -- each compiler does this differently.

O.k i understand. I am using Devx. Compiler do you have any idea how i should link the files together?

>I am using Devx. Compiler do you have any idea how i should link the files together?
There's an "add source file to project" button or something similar to that (been quite a while since I used Dev). Once the CPP files are added, everything should work as expected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.