954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

trouble declaring vectors as data members in class

Hi,
I am trying to write a class that uses vectors that contain another class. When I compile my code using g++ I get the following error that I don't understand:
In file included from cars.cpp:5:
car.h:24: error: syntax error before `;' token
car.h:25: error: syntax error before `;' token

What is wrong with the syntax below?

#ifndef CAR_H
#define CAR_H


class Car {
    public:
        Car(int yr, const char *mk, const char *mod, int maxPass);
        ~Car();
        Person getDriver();
        Person getPassenger();
        int getSpeed();
        int howManyPassengers();
        void turnIgnitionOn();
        void turnIgnitionOff();
        void accelerate(int x);
        void decelerate(int x);
        void addDriver(Person name);
        void removeDriver();
        void addPassenger(Person name);
        void removePassenger();
        void showCar();
    private:
        int  year, maxPassengers, numberOfPassengers, speed;
        std::vector<Person> driver_seat;
        std::vector<Person> passengers;
        char make[20];
        char model[20];
        bool ignitionSwitch;
};

#endif
klactose
Light Poster
43 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

you probably forgot to include header file.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

oh... I didn't realize that I needed to have an include in the header file.. i thought only the .cpp files needed them... thanks!

klactose
Light Poster
43 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

You can do it either way -- include in the *.cpp file before including car.h, or just add at the top of car.h. Either way works.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You