Inheritance - different files (C++) -


i'm trying out inheritance tutorial in different files, .h , .cpp.

i've done necessary #include of header files. couldn't figure out wrong code, when trying run it. have error stating that:

severity code description project file line

error   c2011   'person': 'class' type redefinition      error   c2027   use of undefined type 'person'       error   c2065   'idnum': undeclared identifier   error   c2065   'lastname': undeclared identifier    error   c2065   'firstname': undeclared identifier       error   c2027   use of undefined type 'person'       error   c2065   'idnum': undeclared identifier   error   c2065   'firstname': undeclared identifier       error   c2065   'lastname': undeclared identifier    

below code:

person.h

#include <iostream> #include <string>  using namespace std; class person {  private:     int idnum;     string lastname;     string firstname; public:     void setfields(int, string, string);     void outputdata();   };  void person::setfields(int num, string last, string first) {     idnum = num;     lastname = last;     firstname = first;  }  void person::outputdata() {     cout << "id #" << idnum << " name: " << firstname << " " << lastname << endl; } 

customer.h

#include <iostream> #include <string> #include "person.h"  using namespace std; class customer :public person {  private:     double balancedue; public:     void setbaldue(double);     void outputbaldue(); };  void customer::setbaldue(double bal) {     balancedue = bal; }  void customer::outputbaldue() {     cout << "balance due $ " << balancedue << endl; } 

main.cpp

#include <iostream> #include <string> #include "customer.h" #include "person.h"  using namespace std;  int main() {     customer cust;     //cust.setfields(215, "santini", "linda");     //cust.outputdata();     cust.setbaldue(147.95);     cust.outputbaldue();      return 0;  } 

edit: person.h

#include <iostream> #include <string> #ifndef person_h #define person_h  using namespace std; class person {  private:     int idnum;     string lastname;     string firstname; public:     void setfields(int, string, string);     void outputdata();   };  void person::setfields(int num, string last, string first) {     idnum = num;     lastname = last;     firstname = first;  }  void person::outputdata() {     cout << "id #" << idnum << " name: " << firstname << " " << lastname << endl; } #endif 

function definitions should in .cpp file, not in .h file.

you need this:

customer.cpp

#include <iostream> #include <string> #include "customer.h"  using namespace std;  void customer::setbaldue(double bal) {     balancedue = bal; }  void customer::outputbaldue() {     cout << "balance due $ " << balancedue << endl; } 

main.cpp

#include <iostream> #include <string> #include "customer.h" #include "person.h"  using namespace std;  int main() {     customer cust;     //cust.setfields(215, "santini", "linda");     //cust.outputdata();     cust.setbaldue(147.95);     cust.outputbaldue();      return 0;     } 

person.cpp

#include <iostream> #include <string> #include "person.h"  void person::setfields(int num, string last, string first) {     idnum = num;     lastname = last;     firstname = first;     }  void person::outputdata() {     cout << "id #" << idnum << " name: " << firstname << " " << lastname << endl; } 

in header files need include guards.

customer.h

#ifndef _customer_inc_h_ #define _customer_inc_h_  // include guard. makes sure                           // customer.h included once                           // avoid "multiple definition" errors  #include <iostream> #include <string> #include "person.h"  using namespace std;  class customer :public person {  private:     double balancedue; public:     void setbaldue(double);     void outputbaldue(); };  #endif 

person.h

#ifndef _person_inc_h_ #define _person_inc_h_  #include <iostream> #include <string>  using namespace std;  class person {  private:     int idnum;     string lastname;     string firstname; public:     void setfields(int, string, string);     void outputdata(); }; #endif 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -