Hey all,
I'm trying to write a simple program involving a car class. I start a new Console Application and save my project as Car1. Then the screen with int main appears awaiting my code.
For cleanliness, I put my class code in a separate source file. I did this by pressing ctrl + N and saying "yes" to whether or not I'd like to add a new file to the current project. Then pops up Untitled1 under Car1 on the left side (along with main.cpp). So I bounce over to the Untitled1 screen and write my car class code:
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
|
enum Color {blue, red, yellow, white};
class Car
{
public:
Color PaintColor;
int NumOfDoors;
int GasLevel;
void start_engine()
{
GasLevel -= 1;
}
void take_for_spin(int NumOfMiles)
{
cout << "You took it for a spin of " << NumOfMiles << " miles!" << endl;
GasLevel -= NumOfMiles;
}
void check_gas_level()
{
cout << "Your gas level is currently: " << GasLevel << endl;
}
};
|
Ok, so that's done. Now I switch back over to main.cpp and write the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Car MyCar;
MyCar.PaintColor = red;
MyCar.NumOfDoors = 4;
MyCar.GasLevel = 25;
MyCar.check_gas_level();
MyCar.take_for_spin(22);
MyCar.check_gas_level();
system("PAUSE");
return EXIT_SUCCESS;
}
|
I press F9 to compile and run and it asks me to save the file. What file is it talking about? I already saved it once before I started writing any code. At any rate, I use the default name of "main" and click save. Up pops another save box asking me to save another file, this time Untitled1. I do so.
No more save boxes appear. The first error it gives me is in my main.cpp file, on the first line I wrote in it:
It says that "Car" is undeclared.
The book I'm reading says to save my car class as a .h file. That's all it says though. Where do I save it? Anywhere on my computer?
Not wanting to give up without a fight, I click over to the "Untitled1" file and go to file -> save as, hoping it knows I only want to save that Untitled1 file, not the main.cpp one as well. The save box opens with the default name being Untitled1. I change it to Untitled1.h and save it.
F9 to compile and run and again the same error.
I add "#include "Untitled1.h" at the top of my main.cpp folder and F9 it again, to now receive an error on that line (the #include line). The error message is "In file included from main.cpp", whatever that means.
Can anyone please help me get this simple project working? Where am I going wrong?
Thank you!