c++ - C++:重新定义'class FiguraPlaska'
问题描述
我的运动有问题。如果我添加 Trojkat.h 它不起作用。如果我评论它,那么它的工作原理。我不知道为什么它不起作用。我有 80% 的把握一切都很好,但有些问题……无论如何希望有人能帮助我
FiguraPlaska.h
#include <iostream>
class FiguraPlaska {
protected:
virtual void Wypisz(std::ostream& out) const = 0;
friend std::ostream& operator<<(std::ostream& os, const FiguraPlaska&
figura);
public:
virtual double Pole() = 0;
virtual double Obwod() = 0;
virtual ~FiguraPlaska(); //DESTRUKTOR
};
木马变种h
#ifndef TROJKAT_H
#define TROJKAT_H
#include "FiguraPlaska.h"
class Trojkat : public FiguraPlaska {
double a,b,c;
protected:
void Wypisz(std::ostream& out) const override;
public:
Trojkat(double a, double b, double c);
double GetA() const;
void SetA(double a);
double GetB() const;
void SetB(double b);
double GetC() const;
void SetC(double c);
double Obwod() override;
double Pole() override;
~Trojkat() override;
private:
};
#endif
Prostokat.h
#ifndef PROSTOKAT_H
#define PROSTOKAT_H
#include "FiguraPlaska.h"
class Prostokat : public FiguraPlaska{
private:
double a,b;
protected:
void Wypisz(std::ostream& out) const override;
public:
Prostokat(double a, double b);
double GetA() const;
void SetA(double a);
double GetB() const;
void SetB(double b);
double Obwod() override;
double Pole() override;
~Prostokat() override;
};
#endif
解决方案
您也应该放入类似的包含块FiguraPlaska.h
。
#ifndef FIGURA_H
#define FIGURA_H
class FiguraPlaska ....
#endif
为什么?因为当您实际使用当前代码进行编译时,您包含两次相同的类。
例如,获取一个源文件
#include "prostokat.h"
#include "trojkat.h"
然后在使用您的代码展开包含后,它看起来像这样:
class FiguraPlaska // because of include from prostokat
class Prostokat // because prostokat.h
class FiguraPlaska // because of include from trojkat <- BOOM
class Trojkat // because trojkat.h
推荐阅读
- ios - iOS - AudioToolbox 内存泄漏
- c - 数组如何终止?
- php - 如何在显示页面中删除我的消息代码中的接收者姓名和我们的消息?
- django - 使用 Django CkEditor 时如何解决“缺少图像源 URL”问题
- php - 如何使用 PHP 和 Imagick 向图像添加网格
- outlook - Outlook 无法识别 .ics 文件中的自定义 UID,创建新事件(和新 ID)
- javascript - 如何允许多个业务用户通过 API 为 Angular 应用程序自动生成 Paypal Express 客户端 ID
- javascript - 将数组转换为 Observable 的类型
- php - 使用具有相同列名的 3 个表排序
- authentication - SPA-PWA 身份验证的最佳实践是什么?