3 files (Car.h, Car.cpp, automobile.cpp), is variable declared in .h file accessible in both cpp files?

I have three files and I declared a variable in .h file but my cpp file where main() resides can't recognize this variable. Why?

below is from Car.h file:

class Car
{
// Public Interface

private:


// Variable Declarations
int YearModel, Speed;
std::string Make;

===========================================================================
below are automobile.cpp file that includes main()

include "stdafx.h" // Defines IDE required external definition files
#include <iostream> // Defines objects and classes used for stream I/O
// Class Definition header file(s)
#include "Car.h"
#include <string.h>

// Namespaces utilized in this program
using namespace std; // Announces to the compiler that members of the namespace "std"
// are utilized in this program

int main()
{


char choice; //Menu selection
Speed = 0;

=======================================================

somehow the variable Speed is not recognized by this cpp file. Why?
I don't know... you but you can use [ code ] [ \code ] tags without the spaces to make this easier to read. It's hard for me to discern where one file begins and the other ends with the way you have this formatted.

I will say this though, you shouldn't be including all that stuff in your automobile.cpp file. the only include in automobile.cpp should be the automobile.h include. Everything else you want automobile.cpp to use should be in automobile.h. Think of it this way, when you put include "automobile.h" in your automobile.cpp file, you're actually saying copy and paste the contents of automobile.h into automobile.cpp (that will be happening "behind the scenes" so to speak.


Also, main should also not be in automobile.cpp. (don't know if you're actually doing that)
Last edited on
Firstly, the golden rule with including files is to only include what you need, though, of course, you should include everything you need. Basically, in your Car.h file, the only thing you should include is <string> (and/or maybe stdafx.h), and then put into automobile.cpp the include files you need.

theperson wrote:
Also, main should also not be in automobile.cpp. (don't know if you're actually doing that)

Why not? You do realise that main() doesn't have to be in something like main.cpp?

@OP
The reason is because they have been declared as private variables inside a class. If you want to set the speed, do something like this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// car.h
#include <string>

class Car {
    public:
        int getSpeed() const { return Speed; }
        void setSpeed(int s) { Speed = s; }

        //...

    private:
        int Speed;
        int YearModel;
        std::string Make;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// automobile.cpp
#include "stdafx.h"
#include <iostream>
#include "car.h"

using namespace std;

int main() {
    Car car;
    car.setSpeed(0);
    std::cout << car.getSpeed() << '\n';
    return 0;
}

0


If you want 'main' to recognise the variable, you will need to declare the variable within a cpp file and then declare it in the header file prepended by 'extern', like so:
1
2
3
4
5
// header
extern int Speed;

// cpp file
int Speed = 0;
Last edited on
Thanks everyone who posted. I got it now. :)
Topic archived. No new replies allowed.