Reputation: 414
I'm trying out this inheritance tutorial in different files, .h and .cpp.
I've done the necessary #include
of header files.
I couldn't figure out what is wrong with the code, when I am trying to run it.
It have the 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 is my 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
Upvotes: 3
Views: 1811
Reputation: 50812
Function definitions should be in a .cpp file, not in a .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 the header files you need include guards.
Customer.h
#ifndef _customer_inc_h_
#define _customer_inc_h_ // Include guard. This makes sure that
// Customer.h is included actually only once
// to 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
Upvotes: 3