gpt4 book ai didi

c++ - 继承 - 不同的文件 (C++)

转载 作者:太空狗 更新时间:2023-10-29 21:38:09 24 4
gpt4 key购买 nike

我正在不同的文件 .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

最佳答案

函数定义应该在 .cpp 文件中,而不是在 .h 文件中。

你需要这个:

客户.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

关于c++ - 继承 - 不同的文件 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36057617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com