Help with static member function

Hi everyone, this is my first post here, so please forgive any mistakes made on my part. I am having trouble implementing a static member function for a class called Car. Here is the code in my Car.h file:

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
30
31
32
#ifndef CAR_H
#define CAR_H

#include <string>
#include "Vehicle.h"

using namespace std;

static int carCount = 0;

class Car : public Vehicle
{
public:
	Car();
	Car(string, string, int, int, int);
	~Car();
	void PrintName();
	void PrintTorque();
	void PrintHorsepower();
	static int GetNumberOfCars()
	{
		return carCount;
	}
private:
	string make;
	string model;
	int VIN;
	int horsepower;
	int torque;
};

#endif 


And here is the implementation in Car.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "stdafx.h"
#include <string>
#include <iostream>
#include "Car.h"

using namespace std;

Car::Car()
{
	make = "unknown";
	model = "unknown";
	VIN = 0;
	torque = 0;
	horsepower = 0;
	carCount++;
}

Car::Car(string mk, string mdl, int vIDnum, int trq, int hp)
{
	make = mk;
	model = mdl;
	VIN = vIDnum;
	torque = trq;
	horsepower = hp;
	carCount++;
}

Car::~Car()
{
	carCount--;
}

void Car::PrintName()
{
	cout << "Make: " << make << endl;
	cout << "Model: " << model << endl;
}

void Car::PrintTorque()
{
	cout << "Torque: " << torque << endl;
}

void Car::PrintHorsepower()
{
	cout << "Horsepower: " << horsepower << endl;
}


Everything seems to compile properly, but when I run a simple driver program the carCount variable doesn't seem to increment and the GetNumberOfCars() function returns garbage.

Any help would be greatly appreciated! Thanks!
You should move the static carCount variable inside the Car class. Then you'll need to instantiate the variable in Car.cpp, and then it will work.

As it is, carCount is a global variable, and never declare global variables in header files.
Hi jsmith, thanks for your quick reply. I have moved static int carCount from the header to underneath the private part of class Car. Is that correct?

Also, what do you mean by "instantiate the variable"? Do I need to use the new function somehow?
you haave delcare both your static variables inside the class. to acess your static datamember outside in your class ,
(in car.cpp,may be line 7)

int car :: count;
Thanks to both jsmith and pain sama!! You helped me figure out my problem :)

For any others having the same problems here is the 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
27
28
29
30
31
32
// Car.h
#ifndef CAR_H
#define CAR_H

#include <string>
#include "Vehicle.h"

using namespace std;

class Car : public Vehicle
{
public:
	Car();
	Car(string, string, int, int, int);
	~Car();
	void PrintName();
	void PrintTorque();
	void PrintHorsepower();
	static int GetCarCount()
	{
		return carCount;
	}
private:
	string make;
	string model;
	int VIN;
	int horsepower;
	int torque;
	static int carCount;
};

#endif 


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
30
31
32
33
34
35
36
// Car.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
#include "Car.h"

using namespace std;

int Car::carCount = 0;

Car::Car()
{
	make = "unknown";
	model = "unknown";
	VIN = 0;
	torque = 0;
	horsepower = 0;
	carCount++;
}

Car::Car(string mk, string mdl, int vIDnum, int trq, int hp)
{
	make = mk;
	model = mdl;
	VIN = vIDnum;
	torque = trq;
	horsepower = hp;
	carCount++;
}

Car::~Car()
{
	carCount--;
}

// All that other stuff..... 


Again, thanks to those that helped me! Much appreciated!!
Topic archived. No new replies allowed.