Another Simple Class

okay so i know how to do a class program with just a header file and main. However when it comes to making using an external constructor file, i am stumped.
this is what i have so far. it may look like the one i had before and it is, but Im just using a different method of writing it. Thank you!

trip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef __TRIP_H__
#define __TRIP_H__
using namespace std;

class Trip 
{
  // Public functions go here
public:
  Trip();
  void intro(); 
  float mileage(int miles, float gallons); 
  float tripcost(float costpergallon, float milespergallon, int totaldistance);
private:
  // private data / functions go here (none for now)
   
};


#endif


trip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "trip.h"
using namespace std;
// Code goes here

Trip::Trip(){}

void Trip::intro()
{
     cout << "Hello" << endl;
     cout << "This program calculates the cost of your trip" << endl;        
}

Trip::mileage(int m, int g)
{
     int MPG;
     MPG = m/g;
     return MPG;
}


Trip::tripcost(float cpg, float mpg, int td)
{
     float tcost;
     tcost = cpg * mpg * td;
     return tcost;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

#include "trip.h"
using namespace std;

int main()
{
int miles;
float gallons;
    
Trip trip;

trip.intro();

cout << "Enter the number of Miles and Gallons used: ";
cin >> miles >> gallons;

trip.mileage(miles, gallons);

cout << "Your mpg is: " << trip.MPG << endl;

system("pause");
}


errors
1
2
main.cpp: In function `int main()':
main.cpp:20: error: 'class Trip' has no member named 'MPG' 


- trip.cpp, lines 13, 21: You need to give your functions return types. In your header, you declared them as floats, so these lines should start with float as well.

- trip.cpp, line 13: 'gallons' is supposed to be a float according to your header file. Always make sure your prototype and function body match exactly.

As for the error you're asking about:

1
2
3
trip.mileage(miles, gallons);

cout << "Your mpg is: " << trip.MPG << endl;


Where is that "MPG" coming from? The mileage is returned from your mileage() function call on line 18. If you want to print that, then print it:

 
cout << "Your mpg is: " << trip.mileage(miles,gallons) << endl;
Okay sorry it too so long to reply. So now I guess i want to just output the intro just to make sure the program works. But i get this error:
1
2
3
:main.cpp: undefined reference to `Trip::Trip()'
:main.cpp: undefined reference to `Trip::intro()'
collect2: ld returned 1 exit status


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

#include "trip.h"
using namespace std;

int main()
{
int miles;
float gallons;
    
Trip trip;

trip.intro();

//cout << "Enter the number of Miles and Gallons used: ";
//cin >> miles >> gallons;
//
//trip.mileage(miles, gallons);
//
//cout << "Your mpg is: " << trip.mileage(miles,gallons) << endl;
//
//int CPG;
//cout << "Enter the cost per gallon: ";
//cin >> CPG;
//
//trip.tripcost(CPG, trip.mileage(miles, gallons), miles);

system("pause");
}


trip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef __TRIP_H__
#define __TRIP_H__
using namespace std;

class Trip 
{
  // Public functions go here
public:
  Trip();
  void intro(); 
//  float mileage(int miles, float gallons); 
//  float tripcost(float costpergallon, float milespergallon, int totaldistance);
//private:
//  // private data / functions go here (none for now)
//   
//};


#endif


trip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "trip.h"
using namespace std;
// Code goes here

Trip::Trip(){}

void Trip::intro()
{
     cout << "Hello" << endl;
     cout << "This program calculates the cost of your trip" << endl;        
}

//int Trip::mileage(int m, int g)
//{     
//     return m/g;
//}
//
//float Trip::tripcost(float cpg, float mpg, int td)
//{
//     return cpg * mpg * td;
//} 
Most likely, you are not linking in trip.o

Are you compiling and linking in one shot or in two steps?
Show us your compile/link commands.
Im just using devC++ compiler program.
Ah... upgrade to wxDev-C++ or Code::Blocks, and then we might be able to help you a little further...
(Seriously, Dev-C++ belongs in a museum at this point).

-Albatross
Topic archived. No new replies allowed.