我正在尝试使用不同文件中的继承教程,.h和.cpp。
我已经完成了必要的#include
头文件。
当我试图运行它时,我无法弄清楚代码有什么问题。
它有错误说明:
严重性代码说明项目文件行
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
下面是我的代码:
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;
}
编辑: 的 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
答案 0 :(得分:2)
函数定义应该在.cpp文件中,而不是在.h文件中。
你需要这个:
<强> 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;
}
在标题文件中,您需要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