Simple program giving me an error (source/header files)

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:

 
Car MyCar;


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!

You need to include untitled1.h at the top of your main.cpp. The error is in file included in main (the car class). maybe you aren't including iostream there to use cout.
I think you may have a problem with cout<< as you have given the header file and defined it's namespace in main.cpp, but called it in Untitled1.h.

You might consider moving #include<iostream> & using namespace std;, to the top of Untitled.h.

Just for completeness, if you're not making use of command line parameters:

check2011 wrote:
int main(int argc, char *argv[])
this main function prototype is not required. You should use: int main() with no parameters, if you're not making use of command line parameters.

check2011 wrote:
system("PAUSE");
This is bad for several reasons that are clarified here: http://www.cplusplus.com/forum/beginner/1988/

You'll also find an alternative to it in the same thread (it's in the first reply).
Last edited on
Thanks for the quick response.

I tried adding Untitled1.h at the top of my main.cpp, and got that"In file included from main.cpp" error. I'm not quite sure what you're saying about it.

What do you mean by including iostream? All I added was #include "Untitled1.h" at the top of main.cpp. Should there be "iostream" somewhere in there?

Thanks again. :)
Yep i tested ur code all you need is
1
2
#include <iostream>
using namespace std;

in your untitled1.h
also what compiler are you using? mine says
cout was not declared in this scope
Last edited on
check2011 wrote:
What do you mean by including iostream? All I added was #include "Untitled1.h" at the top of main.cpp. Should there be "iostream" somewhere in there?
check2011 wrote:
1
2
#include <iostream>
using namespace std;
It was already present in your code from your first post.

Firstly, we should call Untitled.h something a little more appropriate: what do you think of Car.h?

In (what I am now calling Car.h) Car.h you have made use of cout<< several times, but at this point the compiler probably doesn't know what it is. This is because cout<< is not a part of standard C++ but rather it's define in the iostream library. So it should be placed in Car.h like this:

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

enum Color {blue, red, yellow, white};
class Car
{
    //code omitted
};


Then your main.cpp should look like this:
1
2
3
4
5
6
7
#include <cstdlib>
#include "Car.h"  //notice they are enclosed with double quotes NOT angle brackets

int main()
{
    //code omitted
}


I hope that clears it up.
Last edited on
Thanks to you both very much. My program works just fine now.

I wonder why Dev-C++'s default code includes that system("PAUSE");? On several of the tutorials I've found online (and even in the book I'm using), it has something like return(0);. Do I just have an outdated version of Dev?

I suppose it'd be nice if it also included the

1
2
#include <iostream>
using namespace std;


in a new source file automatically, but maybe that's something it does in a newer version (a link to the newest version out there would be great).

Very helpful! I'm glad I asked here.
Dev-C++ hasnt updated for years. If you got enough space in you computer you should really download a newer one. There are a plenty of free ones.
What would you recommend? The simpler, the better. A quick Google search turns up Microsoft Visual C++ 2010 Express.
There's a nice article written by Albatross about Dev C++, you can find it here: http://www.cplusplus.com/articles/36vU7k9E/

I use Visual Studio 2008/2011. Normally MS charge extortionately for it, but I get it free because of a licensing agreement between my University and Microsoft. If you're a student you should check to see if you are entitled to some free software.

If not MS offer cut down versions of Visual Studio called "Express Editions", you can find and download it here: http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express

I also hear good things about Code:Blocks but I've never used it so I can't comment on it.

Glad I could help you out.
Topic archived. No new replies allowed.