I have a class Vehicle (vehicle.h and vehicle.cpp). Visual Studio is giving me errors if I don't put the includes inside of the header file. Why is this? In my previous 2 assignments I put the includes in the .cpp file and had no issues. Here is what I want to work but isn't:
vehicle.h:
#ifndef VEHICLE_H
#define VEHICLE_H
using namespace std;
class Vehicle
{
public:
Vehicle();
Vehicle(string mfr, int cylinders, Person owner);
string getManufacturer();
int getNumOfCylinders();
Person getOwner();
private:
string manufacturer;
int numOfCylinders;
Person owner;
};
#endif
vehicle.cpp:
#include <iostream>
#include <string>
using namespace std;
#include "person.h"
#include "vehicle.h"
Vehicle::Vehicle()
{
//Initialize with defaults
}
Vehicle::Vehicle(string mfr, int cylinders, Person owner)
{
//Initialize with parameters
}
string Vehicle::getManufacturer()
{
return manufacturer;
}
int Vehicle::getNumOfCylinders()
{
return numOfCylinders;
}
Person Vehicle::getOwner()
{
return owner;
}
person.h:
#ifndef PERSON_H
#define PERSON_H
class Person {
public:
//default constructor
Person();
//constructor with two parameters
Person(string the_name, string no);
//accessor member functions
string get_name() const;
string getDriverLicenseNo() const;
//overloaded equal operator
bool operator==(const Person& p);
//overloaded extraction operator
friend istream& operator >> (istream& in, Person& p);
//overloaded insertion operator
friend ostream& operator <<(ostream& out, Person& p);
private:
string name;
string drivingLicenseNo;
};
#endif
person.cpp:
#include <iostream>
#include <string>
using namespace std;
#include "person.h"
//default constructor
Person::Person(){}
//constructor with two parameters
Person::Person(string the_name, string no){}
//accessor member functions
string Person::get_name() const
{
return name;
}
string Person::getDriverLicenseNo() const
{
return drivingLicenseNo;
}
//overloaded equal operator
bool Person::operator==(const Person& p)
{
return false;
}
//overloaded extraction operator
istream& operator >> (istream& in, Person& p)
{
return in;
}
//overloaded insertion operator
ostream& operator <<(ostream& out, Person& p)
{
return out;
}
truck.h:
#include "vehicle.h"
class Truck: public Vehicle
{
};
truck.cpp:
#include "person.h"
#include "vehicle.h"
//implement truck here
Couple of errors for example:
error C2061: syntax error : identifier 'string'
error C2146: syntax error : missing ';' before identifier 'getManufacturer'
error C2061: syntax error : identifier 'string'
error C2535: 'Person::Person(void)' : member function already defined or declared
error C2146: syntax error : missing ';' before identifier 'get_name'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error : missing ';' before identifier 'getDriverLicenseNo'
using namespace std;in a header, ever. - Nathan Ernst