首页 > 解决方案 > 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

标签: c++

解决方案


您也应该放入类似的包含块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

推荐阅读