String Variable not Recognized?

Sorry for posting another question on the same day... but I've run out of ideas for this one.

Basically, I'm making a simple program with a class (Car) and an object of that class (carA) with a header file, a class definition file, and the main program file. From the error I'm getting, error C2146: syntax error : missing ';' before identifier 'make' (error in line 9 of Car.h), it makes me think that the string type for one of the class members isn't being recognized or something, and I do have the #include <string> added. I'll post the three files of the program below.

Code for file: Problem.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
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;

int main()
{
	Car carA;
	int count = 0;

	while (count < 5)
	{
		carA.accelerate(sp);
		cout << "Car's speed after acceleration: " << sp << endl;
		count++;
	}
	cout << endl;
	count = 0;
	while (count < 5)
	{
		carA.brake(sp);
		cout << "Car's speed after breaking: " << sp << endl;
		count++;
	}
	return 0;
}




Code for Car.h
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
#ifndef DATE_H
#define DATE_H

class Car
{
	private:
		int year,
			speed;
		string make;

	public:
		Car(year, make)
		{
			year = year;
			speed = 0;
			make = make;
		}

		int getYear(int yr)
		{
			yr = year;
			return yr;
		}
		int getSpeed(int sp)
		{
			sp = speed;
			return speed;
		}
		string getMake(string mk);
		{
			mk = make;
			return make;
		}
		
		int accelerate(int);        // Defined in Car.cpp
		int brake(int);             // Defined in Car.cpp
};
#endif 




Code for Car.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Car.h"
#include <string>
#include <iostream>

int Car::accelerate(int sp)
{
	sp += 5;
	return sp;
}

int Car::brake(int sp)
{
	sp -= 5;
	return sp;
}
Try to replace "string" with "std::string" in the header file.
AND:

Put #include "Car.h" FIRST in the cpp file. This will give you another error, because std::string isn't found when reading the header file. Don't worry about that, just add #include <string> in the header file (then you may remove it from the source file, but it doesn't matter).

It is a very good idea to always have your own header files first in the include list, because it will force you to include all necessary libraries in the header files. This makes it possible to include your header files in other files without getting errors.

EDIT: I see now that you already have "Car.h" first in the Car.cpp file, so you will have to include <string> in the header file to get it working even if you don't change the other cpp file.
Last edited on
Uhm... x_x;

Ok I tried including <string> in the header file, and it didn't work, and when I tried the std::string it said that std did not exist as a namespace. I also moved the include of my header file to the top in my main .cpp file. Still not working.

Any other ideas or help?
Last edited on
You have to include <string> in the header file AND replace ALL "string" with "std::string". The include should be between #define DATE_H and before class Car.

If that doesn't work, post your updated code, and the error message.

BTW: You should change DATE_H to something like CAR_H.
Alright I got it working with that, thanks for the help!
Topic archived. No new replies allowed.