Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ where to place includes
    primarykey
    data
    text
    <p>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:</p> <p>vehicle.h:</p> <pre><code>#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 </code></pre> <p>vehicle.cpp:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; 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; } </code></pre> <p>person.h:</p> <pre><code> #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&amp; p); //overloaded extraction operator friend istream&amp; operator &gt;&gt; (istream&amp; in, Person&amp; p); //overloaded insertion operator friend ostream&amp; operator &lt;&lt;(ostream&amp; out, Person&amp; p); private: string name; string drivingLicenseNo; }; #endif </code></pre> <p>person.cpp:</p> <pre><code> #include &lt;iostream&gt; #include &lt;string&gt; 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&amp; p) { return false; } //overloaded extraction operator istream&amp; operator &gt;&gt; (istream&amp; in, Person&amp; p) { return in; } //overloaded insertion operator ostream&amp; operator &lt;&lt;(ostream&amp; out, Person&amp; p) { return out; } </code></pre> <p>truck.h:</p> <pre><code>#include "vehicle.h" class Truck: public Vehicle { }; </code></pre> <p>truck.cpp:</p> <pre><code>#include "person.h" #include "vehicle.h" //implement truck here </code></pre> <p>Couple of errors for example:</p> <p>error C2061: syntax error : identifier 'string'</p> <p>error C2146: syntax error : missing ';' before identifier 'getManufacturer'</p> <p>error C2061: syntax error : identifier 'string' error C2535: 'Person::Person(void)' : member function already defined or declared<br> error C2146: syntax error : missing ';' before identifier 'get_name'</p> <p>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int</p> <p>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int</p> <p>error C2146: syntax error : missing ';' before identifier 'getDriverLicenseNo' </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload