所以我正在为我的学校作业练习继承,但我无法正确实施标题保护

So I am practicing inheritance for my school Assignment but I am not being able to properly implement the header guards

本文关键字:施标题 标题 保护 我的 学校 作业 继承 练习      更新时间:2023-10-16

我有一张具有基本卡属性的基类卡。总的来说,我得到一个重新定义的错误,因为类呼叫卡和 IDCard 都继承了基类卡。我尝试使用标题保护,但我无法正确实现它们。同样在使用 G++ 编译器时,我需要包含".cpp"而不是".h",因为包含".h"会给我一个错误

建筑x86_64的未定义符号:

主 ad117f.o 中的_main (int, std::__1::basic_string, std::__1:::allocator>, std::__1::basic_string, std::__1::allo cator>, int, std::__1::basic_string, std::__1:::allocator>, int(", 引用自:

主 ad117f.o 中的_main

LD:找不到建筑x86_64符号

clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用(

它不可能是标题保护,可能是在 main 中包含".cpp"而不是".h",但这给了我一个我不知道如何解决的错误 请帮助我解决任何问题,无论是标题保护还是包含".cpp"而不是".h">

卡.h

这是我的基类

#include<iostream>
#include<string.h>
using namespace std;
#ifndef CARD_H           //Header guards or pragma once? and how to implement?
#define CARD_H
class Card
{
private:
int cardNumber;
protected:
string ownerName;
public:
string expiryDate;
Card(int cardNumber=0, string ownerName="Non", string expiryDate="0");
void display();
};
#endif

卡.cpp

#include"Card.h"
Card::Card(int cardNumber, string ownerName, string expiryDate)
{
this->cardNumber=cardNumber;
this->ownerName=ownerName;
this->expiryDate=expiryDate;
}
void Card::display()
{
cout<<"Card Number: "<<this->cardNumber<<endl;
cout<<"Owner Name: "<<this->ownerName<<endl;
cout<<"Expiry Date: "<<this->expiryDate<<endl;
}

呼叫卡.h

这是一个从基类继承的子类

#include"Card.cpp"
#ifndef CALLINGCARD_H
#define CALLINGCARD_H
class CallingCard :
public Card
{
private:
int amount;
string companyName;
int PIN;
public:
CallingCard(int cardNumber, string ownerName, string expiryDate, int amount=0, string companyName="Non", int PIN=0);
void display();
};
#endif

呼叫卡.cpp

#include"CallingCard.h"
CallingCard::CallingCard(int cardNumber, string ownerName, string expiryDate, int amount, string companyName, int PIN):Card(cardNumber,ownerName,expiryDate)
{
this->amount=amount;
this->companyName=companyName;
this->PIN=PIN;
}
void CallingCard::display()
{
Card::display();
cout<<"Amount: "<<this->amount<<endl;
cout<<"Company Name: "<<this->companyName<<endl;
cout<<"PIN: "<<this->PIN<<endl;
}

IDCard.h

这是从基类继承的另一个子类

#include"Card.cpp"
#ifndef IDCARD_H
#define IDCARD_H
class IDCard :
protected Card
{
private:
int CNICNumber;
int age;
public:
IDCard(int cardNumber, string ownerName, string expiryDate, int CNICNumber=0, int age=0);
string getExpiryDate() const;
void display();
};
#endif

IDCard.cpp

#include"IDCard.h"
IDCard::IDCard(int cardNumber, string ownerName, string expiryDate, int CNICNumber, int age):Card(cardNumber,ownerName,expiryDate)
{
this->CNICNumber=CNICNumber;
this->age=age;
}
void IDCard::display()
{
Card::display();
cout<<"CNIC Number: "<<this->CNICNumber<<endl;
cout<<"Age: "<<this->age<<endl;
}

主.cpp

#include"CallingCard.cpp"
#include"IDCard.cpp"
int main()
{
CallingCard card(1234, "Sana", "10-2-2018", 1000, "Warid", 444);
card.display();

IDCard idCard(1234, "Sana", "10-2-2018", 35202, 29);
idCard.display();

}

标头保护必须保护整个文件,因此它们必须是第一件事,也是最后一件事:

// Nothing before the guard
#ifndef NAME_OF_HEADER
#define NAME_OF_HEADER
// All the stuff (also all of the includes!)
#endif
// Nothing after the guard

或者只是

// Nothing before the guard
#pragma once
// All the stuff

这不是标准的一部分,但大多数编译器都支持。

其次,永远不要包含.cpp文件。仅包含标头。

我得到了答案。 所以基本上标题护卫没有任何问题,我的意思是当然也应该包括这些,但也包含".cpp"文件而不是".h"文件。我正在使用带有VSC的Macbook。发生的情况是,我因为在VSC中没有像VS那样创建项目,因此,我需要显式编译所有".cpp"文件。所以我现在所做的是主要的,我包含了".h"文件而不是使用".cpp"(这是错误的(,并使用了命令 g++ Card.cpp CallingCard.cpp IDCard.cpp main.cpp -o a.out 它创造了奇迹。