Hello,

I'm having trouble compiling a header file for a class written in C++. What's so frustrating is that I can't see what I'm doing wrong. when I compile the code I get the the following 2 errors for lines 22 and 25:
expected ';' before '<' token
ISO C++ forbids declaration of 'vector' with no type

In line 22 I want to return a vector of pointers to a class Car. And in line 25 I am trying to declare a class member to be a vector of pointers to class Car. But I don't understand why I would place an extra ';' on those lines. And I thought that I was declaring vectors of Car* properly by using the syntax vector<Car*>.

Below is the entire header file can anyone spot what I am doing wrong?

#ifndef PERSON_H
#define PERSON_H

using std::string;


class Car;

class Person {
	public:
		Person();
		Person(int ag, bool sx, string fn, string ln);
		~Person();
		int getAge();
		std::string getName();
		std::string getFname();
		std::string getLname();
		bool getSex();
		void buyCar(string personName, int year, string carModel);
		Car* sellCar(Car *carPtr);
		void listCars();
		vector<Car*> getCars();

	private:
		vector<Car*> carsOwned;
		int age;
		std::string fname;
		std::string lname;
		bool sex; // true = femaie, false = male

};

#endif

You haven't told the compiler what a vector is, so it considers it an unknown type and goofs-up.

Either add a using std::vector; somewhere at the top, or say std::vector <Car*> foo(); Hope this helps. (Isn't it annoying when the silly stuff gets you?)

[edit] BTW, the header file should also #include <string> #include <vector> and not rely on the using program to do it for you.

Thank you Duoas,

I guess I keep getting confused about where to add the includes. For some reason I thought I was supposed to be adding them to the .cpp files instead of the header files.

Some notes:
1. Use const std::string& parameters where possible (everywhere in this class). Return const std::string& from getLname and getFname members .
2. Car* sellCar(Car *carPtr); - extremelly unsafe member. You made car list (vector) private (that's OK) but allow user to sell any Car from the outer world via pointer. Return const vector<Car*>& from getCars.
3. female == true, I agree. But this fact may be not so evident for tired out programmers. Better add enum Sex { Male, Female }; in the class declaration.
4. Add const modifier to all proper member functions, use inline definition where possible. For example:

int getAge() const  { return age; }
const std::string& getFname() const { return fname; }

5. What's personName parameter in buyCar member? Consider a possible use case for the Person class: we have person1 and person2, person1 sell a car to person2. You may get Car* pointer from person1 but where is arguments for person2.buyCar() in that case?

This article has been dead for over six months. Start a new discussion instead.