Header file Error...

I need to write a program that stores user input in a vector<Person*> and a vector<Car*>. The program then traverses a vector of Person objects and adds 1 year to their ages and then traverses a vector of cars and prints out information about the Car objects.

I am getting an error that my headers do not name types. I don't understand what this means. Any help is appreciated. Code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Person header file

#ifndef PERSON_H
#define PERSON_H

#include<string>

using namespace std;

Class Person
{ public: 
          Person();
          Person(string person_name, int initial_age);
          void raise_age(double new_age);
          string get_name const;
          int get_age const;
          
  private:
          int age;
          string name;
};


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
//Person function definitions and constructors

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

using namespace std;

Person:Person()//default constructor for Person
{ age = 0;
}

Person::Person(string person_name, int initial_age)//overloaded constructor for Person
{ name = person_name
  age = initial_age
}

void Person::raise_age(double new_age)//raise age of Person by 1 year
{ age = new_age + 1;
}

string Person::get_name const;//get_name accessor function of Person
{ return name;
}

int Person::get_age const;//get_age accessor function of Person
{ return age;
}


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
//Car header file

#ifndef CAR_H
#define CAR_H

#include<string>


using namespace std;

Class Car
{ public: 
          Car();
          Car(person* driver, person* owner, string model);
          string get_model() const;
          void print();
          Person* get_owner();
          Person* get_driver();
          
  private:
          Person* driver;
          Person* owner;
          string model;
};


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
//Car function definitions and constructors

#include<iostream>
#include<vector>
#include<string>
#include "car.h"
#include "person.cpp"
#include "person.h"

using namespace std;

Car::Car()
{ }

Car::Car(person* driver, person* owner, string model)
{ d = driver;
  o = owner;
  m = model;
}

Car::get_model() const
{ return model;
}

Car::print()
{ cout << "Model = " << model << endl;
  cout << "Driver's name = " << driver->get_name() << endl;
  cout << "Driver's age = " << driver->get_age() << endl;
  cout << "Owner's name = " << owner->get_name() << endl;
  cout << "Owner's age = " << owner->get_age() << endl;
}
            
Car::get_owner(Person* o) 
{  return Person* o;
}

Car::get_driver(Person* d)
{ return Person* d;
}


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
//Main Function

#include<iostream>
#include<vector>
#include<string>
#include "car.h"
#include "person.cpp"
#include "person.h"

using namespace std;

int main()
{ 
  vector<Person*> driver;
  vector<Person*> owner;
  vector<Car*> cars;  
    
  cout << "Enter person's name: " << endl;
  getline(cin,name);
  cout << "Enter person's age: " << endl;
  cin >> age;
  driver.push_back(name, age);
  owner.push_back(name, age);
  cout <<"Enter model of car: " << endl;
  getline(cin,m);
  cars.push_back(m);
  
  for (i=0; i < driver.size(); i++)
   { driver[i].get_age();
     driver[i].raise_age();
   }
  for (i=0; i < owner.size(); i++)
   { owner[i].get_age();
     owner[i].raise_age();
   }
  
  for (i=0; i < cars.size(); i++)
   { cars[i]->print();
   }
   
system("pause");
return 0;

} 
  
  
car.h should #include person.h
Thanks helios, but I'm still getting the same error after I fixed. Does it have something to do with my main()?

1
2
3
vector<Person*> driver;
  vector<Person*> owner;
  vector<Car*> cars;  
And then you'll encounter...

1. The inclusion guards do not have corresponding #endifs.
2. class is a C++ keyword, Class is not.
3. You're missing some semicolons (but you'll find them by compiling).
4. Do not include .cpp files.
5. using namespace std; is general frowned upon in header files (although for small assignments, I doubt you'll run into any trouble).
6. In Car.cpp, person is not a type; Person is. Also note the inconsistent capitalization of file names and class names.
7. get_age and get_name should have empty argument lists, like: get_age().
8. You mix >> and getline, which will probably cause you a headache.
And then you'll encounter...

1. The inclusion guards do not have corresponding #endifs.
2. class is a C++ keyword, Class is not.
3. You're missing some semicolons (but you'll find them by compiling).
4. Do not include .cpp files.
5. using namespace std; is general frowned upon in header files (although for small assignments, I doubt you'll run into any trouble).
6. In Car.cpp, person is not a type; Person is. Also note the inconsistent capitalization of file names and class names.
7. get_age and get_name should have empty argument lists, like: get_age().
8. You mix >> and getline, which will probably cause you a headache.


Thank you.
1. Fixed
2. Fixed
3. Fixed
4. Fixed
5. Fixed
6. Fixed
7. Fixed
8. Wasn't there a forum on this board about just this? I'll have to search for it.

I'm seeing issues I couldn't see before because I had so many.

1. 12 G:\C++\Lab3\person.h expected `)' before "person_name"
2. 12 G:\C++\Lab3\car.h `string' has not been declared
3. 13 G:\C++\Lab3\car.h `string' does not name a type
4. 21 G:\C++\Lab3\car.h `string' does not name a type


This is just a start, but any help with these would be greatly appreciated...
Do you have #include <string> ?
Those errors look as though you tried to use a string but didn't add the include file.

Edit:
question #8:
http://www.cplusplus.com/forum/articles/6046/
Last edited on
I think it's because OP wrote "#include<string>", rather than "#include <string>".
I changed per your instructions helios and it's still giving me the same compile errors...
Repost what you have so we can spot the problem quickly.
Topic archived. No new replies allowed.