I'm having a bit of trouble here with this class program. I've tried inputting the prototype for the the main function but it's not working either, so I'm a a little lost here.
The instruction was to input the class file called trip.h into a trip.cpp file which is below. Then complete the main function.
trip.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef __TRIP_H__
#define __TRIP_H__
class Trip
{
// Public functions go here
public:
Trip();
void intro();
float mileage(int miles, float gallons);
float tripcost(float costpergallon, float mpg, int totaldistance);
private:
// private data / functions go here (none for now)
};
#endif
#include "trip.h"
#include <iostream>
usingnamespace std;
int main(int miles, float gallons)
{
Trip t1;
t1.intro();
cout << "Enter the number of miles driven and the gallons of gas used." << endl;
cout << "Miles: ";
cin >> miles;
cout << "Gallons: ";
cin >> gallons;
mileage(miles, gallons);
cout << "Miles per gallons: " << mileage();
cout << "What was the cost per gallons?" << endl;
cin >> costpergallon;
tripcost(costpergallon, mileage, miles);
cout << "The total cost of your trip is: $" << tripcost() << endl;
}
This is the compile error that I get:
1 2 3 4 5
In function `int main(int, float)':
`mileage' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
`costpergallon' undeclared (first use this function)
`tripcost' undeclared (first use this function)
Yeah i was kinda confused by that too- how i was to put the header into the trip.cpp file. But i think i should be assuming that the header file is different from the trip.cpp file and I rewrite the trip.cpp file with its own codes right?
We have not learned about inline functions yet, so i wanted to just stick with simple classes first.
The inline is just an example of how to define a member function outside the class. IE: Inside a header file dedicated to member functions for example.
If you make trip.cpp a header file then you will have to create a new header file, rename it and put the trip.cpp contents into it.
A header file does not need #include "trip.h"
just put
1 2
#ifndef FILENAME_H
#define FILENAME_H
//code
Then after the code #endif
If you make it a .cpp file then you dont need the code shown above, instead you put exactly the same headers as you've got in your main.cpp. Also if you do make another .cpp you DONT have to put #include whatevername.cpp in the main.cpp.
See how you go after that, because I think there are then going to be other slight problems with declarations of mileage and costpergallon. But get the files sorted first.
As a general rule, use your .h files for definition, and the .cpp files for implementation of the class. and don't include the .cpp file.
int main(int miles, float gallons)
Thats not quite how you pass paramaters to main().
However from the look of the rest of your code (you would just be overwriting any paramaters anyway in lines 13 and 15) you seem to just be using them as variables.
Also it seems your program doesnt return a value, as you've declared main to return a type int you really should
In which case why not.
1 2 3 4 5 6 7
int main()
{
int miles;
float gallons;
.....
return 0; //endmain
}
Whats the difference between a header file and a class file? Should i even use trip.cpp? Since it seems im not even calling back to it. Sorry, im just a bit confused by the trip.cpp. When i look at the main function i can see that each function is calling back to the .h file, but i dont see trip.cpp into play here at all.
The header file says how 'external' data is structured (It defines classes etc. for the main code). The cpp files are independently compiled and then in the final step of creating the binary linked together so that the classes defined in the header are there for real. If you forget a class member in the cpp class file, you'll get a loinking error in case you try to use it, if you forget one in the .h header, you'll get a compilation error in case you try to use it.
1. Semicolons at end of lines 22,17 and 27 of your original trip.cpp code. Wrong place.
2. Member function name was same as member name, ie; tripcost.
3. You didnt declare costpergallon in main.cpp
4. When calling a member function you need to precede it with the object name and dot operator.
ie; t1.tripmeter(costpergallon, sum, miles);
5. Calculation should be multiplication * not + but thats your own decision anyway as to how you want to define the actual maths. Note that the calculation has been broken down with parenthesis, try googling-> "mathematical precedence". If parenthesis are not used you will get mathematical errors in the calculation.
#include <iostream>
usingnamespace std;
#include "trip.h"
int main()
{
Trip t1;
t1.intro();
int miles;
float gallons;
cout << "Enter the number of miles driven and the gallons of gas used.\n";
cout << "Miles: ";
cin >> miles;
cout << "Gallons: ";
cin >> gallons;
t1.mileage(miles, gallons);
float sum = t1.tripcalc;
cout << "Miles per gallons = " << sum <<"\n";
float costpergallon;
cout << "What was the $ cost per gallon?" << endl;
cin >> costpergallon;
t1.tripmeter(costpergallon, sum, miles);
cout << "The total cost of your trip is: $" << t1.tripcost << endl;
cin.ignore().get();
return 0;
}
If you note that trip.cpp is almost identical to the trip.h I posted, with a few minor changes. You'd just got them confused.
Its actually your ORIGINAL trip.h that is not needed. It had all been done in other files.
What does the function member tripcalc() and tripcost() do? Do i really need to declare two new ints of tripcalc and tripcost? Why cant i just return mileage = mile/gallons like how i had before?
Well first of all, I wanted to rewrite my code my own way and see how it turned out. I know there are mistakes, but I just wanted to write them exactly how I would see it and then hopefully understand from my errors. ;)
To me it looks very logical. But can you list all the errors and explain why they are errors? Thank you. Im trying hard to understand the aspects of class.
1. First off you have declared your floats and ints in the cin.
ie; cin >> int miles;
Look at my example all such variables are declared.
2. Missing semi colons at the end of line 24 and 16.
3. You are passing MPG in line 24. Why and what is it? Have a look at the code I sent. The 'mpg' in that was 'sum'. Have a look at how 'sum was and how it was calculated. It called the member function which returned the amount in 'sum'. It was then passed to the member function tripmeter along with two other arguments and another calculation made. In your resubmitted code you have totally ignored the float sum = t1.tripcalc; call. Sure, call it mpg = t1.tripcalc; whatever, but you still need it!
4. Tripcost and tripcalc, in the code that I sent you, are simply 'member variables' of the Trip class.
If you want to pass arguments to a function and get a return you need those members.
Your code will just not work. Basically the code I sent to you is YOUR style but done in a way to enable it to work. My style of doing this code would actually be different, so dont worry that it was not entirely yours. Its basically the same with the errors fixed thats all. The code you resubmitted has almost gone back to where you were at the start.
Also line 18.
t1.mileage is a function not a variable. If you want it to print out the calculated 'mileage' from line 16, you need to store the result of the function's calculation in a variable (In this case its the function's return value) and display that variable e.g.
Or as the calculation is the return value you could simply print it to the screen using. cout << "Miles per gallon: " << t1.mileage(miles,gallons) << endl;
I would use the first option as you then go on to use your calculated mileage as a parameter to the tripcost function.
My advice would be to forget the classes for now, make sure you understand what you want to do in your head, and code it with standard functions.
Then once you are familiar with functions start work on classes.