I'm new to classes and object-oriented programming. Our instructor is having us create a program that must have a .cpp
file, a main .cpp
file, and a .hpp
file.
Here are each of the files:
First, the odometer.hpp
file:
class Odometer
{
int miles;
float gallons, mpg;
public:
//Constructors
Odometer(); //Default
Odometer(float g, int m);
//Mutator Functions
void Set_miles(int m);
void Set_gallons(float g);
//Functions
void Add_trip(int m, float g);
int Check_mileage(float g);
void Print_info();
//Accessor Functions
float Get_mpg();
float Get_gallons();
int Get_miles();
};
Next, the odometer.cpp
file:
#include "odometer.hpp"
#include <iostream>
//Constructors
Odometer::Odometer()
{
miles = 0;
gallons = 0.0;
mpg = 0.0;
}
Odometer::Odometer(float g, int m)
{
miles = m;
gallons = g;
mpg = m / g;
}
//Mutator functions
void Odometer::Set_miles(int m)
{
miles = m;
}
void Odometer::Set_gallons(float g)
{
gallons = float(g);
}
//Accessor functions
float Odometer::Get_mpg()
{
return mpg;
}
float Odometer::Get_gallons()
{
return gallons;
}
int Odometer::Get_miles()
{
return miles;
}
//Other functions
//Takes # of gallons & # of miles and adds it to previous values, calculating
//new miles/gallon for whole trip
void Odometer::Add_trip(int m, float g)
{
miles += m;
gallons += g;
mpg = miles / gallons;
}
int Odometer::Check_mileage(float g)
{
int newMiles = g * mpg;
return newMiles;
}
void Odometer::Print_info()
{
std::cout << "Miles: " << miles << " Gallons: " << gallons <<
" Miles/Gallon: " << mpg;
}
And finally, the odometer_main.cpp
file (so far, it's incomplete):
#include <iostream>
#include "odometer.cpp"
using namespace std;
int main(void)
{
//Odometer odDefault; //Odometer object set to defaults
Odometer od(10, 100); //Odometer object with values set
return 0;
}
These are the errors I'm getting when I try compiling all the files:
/tmp/ccArjYHP.o: In function 'Odometer::Odometer()': odometer_main.cpp:(.text+0x0): multiple definition of 'Odometer::Odometer()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Odometer()': odometer_main.cpp:(.text+0x0): multiple definition of 'Odometer::Odometer()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Odometer(float, int)': odometer_main.cpp:(.text+0x30): multiple definition of 'Odometer::Odometer(float, int)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x30): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Odometer(float, int)': odometer_main.cpp:(.text+0x30): multiple definition of 'Odometer::Odometer(float, int)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x30): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Set_miles(int)': odometer_main.cpp:(.text+0x72): multiple definition of 'Odometer::Set_miles(int)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x72): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Set_gallons(float)': odometer_main.cpp:(.text+0x8a): multiple definition of 'Odometer::Set_gallons(float)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x8a): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Get_mpg()': odometer_main.cpp:(.text+0xa8): multiple definition of 'Odometer::Get_mpg()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xa8): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Get_gallons()': odometer_main.cpp:(.text+0xbc): multiple definition of 'Odometer::Get_gallons()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xbc): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Get_miles()': odometer_main.cpp:(.text+0xd0): multiple definition of 'Odometer::Get_miles()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xd0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Add_trip(int, float)': odometer_main.cpp:(.text+0xe0): multiple definition of 'Odometer::Add_trip(int, float)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xe0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Check_mileage(float)': odometer_main.cpp:(.text+0x140): multiple definition of 'Odometer::Check_mileage(float)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x140): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Print_info()': odometer_main.cpp:(.text+0x168): multiple definition of 'Odometer::Print_info()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x168): first defined here collect2: error: ld returned 1 exit status makefile:2: recipe for target 'odometer' failed make: *** [odometer] Error 1