为什么编译器会在类中引发重定义错误?

时间:2020-03-29 13:22:56

标签: c++ class oop

我在重新定义时遇到问题,我曾经尝试#pragma和#ifndf语句,但是这些都不适合我。 我的main.cpp


#include <iostream>
#include "kolo.h"
#include "prostokat.h"
#include "trojkat.h"
using namespace std;
//overloading
void zmienno(float x)
{
    cout<<x;
}
void zmienno(int x)
{
    cout<<x;
}
int main()
{
    //tworzenie obiektu zmienna lokalna
    Kolo kolo(5);
    Trojkat trojkat(1,2,3);
    Prostokat prostokat(1,2);
    //wskaznik
    Kolo * wskkolo = new Kolo(5);
    Trojkat * wsktrojkat = new Trojkat(1,2,3);
    Prostokat * wskprostokat = new Prostokat(1,2);
    //konstruktory zaprezentowane wyżej, metody np:
    double pole = kolo.Pole(); //pole kola
    double obwod = wsktrojkat->Obwod(); //obwod trojkata
    kolo.SetR(3);
    pole = kolo.Pole(); // inne pole po zmiane R
    FiguraPlaska *wsk[3];
    wsk[0]=wskkolo;
    wsk[1]=wskprostokat;
    wsk[2]=wsktrojkat;
    for(int i=0;i<3;i++)
    {
        cout<<wsk[i]->Pole()<<endl;//metody wirtualne
    }
    wskprostokat->~Prostokat();
    wskkolo->~Kolo();
    wsktrojkat->~Trojkat();
    // overriding -> polimorfizm metody pole, obwod
    // overloading: funkcje w linii nr8
    zmienno(2);
    //ta sama nazwa funkcji, rozszerzona o inne typy argumentow
    delete[]wsk;
    delete wskkolo,wskprostokat,wsktrojkat;



    return 0;
}

和我的头文件之一一样,每个文件的结构都一样,包括figuraplaska.h


#pragma once
#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;
};

和figuraplaska.h



#include <iostream>
using namespace std;

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();
};

错误是


    [Running] cd "/home/bj/Pulpit/PO1/" && g++ main.cpp -o main && "/home/bj/Pulpit/PO1/"main
In file included from prostokat.h:2:0,
                 from main.cpp:3:
figuraplaska.h:5:7: error: redefinition of ‘class FiguraPlaska’
 class FiguraPlaska {
       ^~~~~~~~~~~~
In file included from kolo.h:2:0,
                 from main.cpp:2:
figuraplaska.h:5:7: note: previous definition of ‘class FiguraPlaska’
 class FiguraPlaska {
       ^~~~~~~~~~~~
In file included from trojkat.h:2:0,
                 from main.cpp:4:
figuraplaska.h:5:7: error: redefinition of ‘class FiguraPlaska’
 class FiguraPlaska {
       ^~~~~~~~~~~~
In file included from kolo.h:2:0,
                 from main.cpp:2:
figuraplaska.h:5:7: note: previous definition of ‘class FiguraPlaska’
 class FiguraPlaska {
       ^~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:45:13: warning: deleting array ‘wsk’
     delete[]wsk;
             ^~~

[Done] exited with code=1 in 2.043 seconds


我不知道如何修复它,有什么建议吗?互联网上没有任何帮助对我有用:/

1 个答案:

答案 0 :(得分:3)

在figuraplaska.h的开始阶段添加

#ifndef FIGURAPLASKA_H_
#define FIGURAPLASKA_H_

最后添加

#endif

确保您的文件内容不会在每个源文件中两次包含

对其他头文件执行类似的操作(当然使用其他宏)


因此 figuraplaska.h 成为了例子

#ifndef FIGURAPLASKA_H_
#define FIGURAPLASKA_H_

#include <iostream>
using namespace std;

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();
};

#endif

除此之外,您在一行中有问题

delete wskkolo, wskprostokat, wsktrojkat;

因为仅删除了 wsktrojkat :用逗号分隔的表达式的值是最后一个表达式的值

替换此行

delete wskkolo;
delete wskprostokat;
delate wsktrojkat;

另一个问题是您之前有以下几行:

wskprostokat->~Prostokat();
wskkolo->~Kolo();
wsktrojkat->~Trojkat();

删除它们,在执行删除wskprostokat 时,将应用〜Prostokat

并删除该行

delete [] wsk;

因为 wsk 放置在堆栈中,所以没有分配在堆中:

FiguraPlaska *wsk[3];