Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

Задача: ShapesDetector Компилятор: g++17 7.3 controlled Вердикт: Ошибка при компиляции…

  1. User

    Задача: ShapesDetector Компилятор: g++17 7.3 controlled Вердикт: Ошибка при компиляции Статус: contest.submits.status.wrong

    #include

    enum class ShapeType { CIRCLE, ELLIPSE, SQUARE, RECTANGLE };

    class Shape { public: virtual double area() = 0; virtual ShapeType GetShapeType() const = 0; };

    class Ellipse : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::ELLIPSE; } };

    class Circle : public Ellipse { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::CIRCLE; } };

    class Rectangle : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::RECTANGLE; } };

    class Square : public Rectangle { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::SQUARE; } };

    bool IsCircle(const Shape& shape) { return shape.GetShapeType() == ShapeType::CIRCLE; }

    bool IsEllipse(const Shape& shape) { return shape.GetShapeType() == ShapeType::ELLIPSE; }

    bool IsSquare(const Shape& shape) { return shape.GetShapeType() == ShapeType::SQUARE; }

    bool IsRectangle(const Shape& shape) { return shape.GetShapeType() == ShapeType::RECTANGLE; }

    stdout:

    stderr: temp.cpp:56:7: error: redefinition of ‘class Shape’ class Shape { ^~~~~ temp.cpp:15:8: note: previous definition of ‘class Shape’ struct Shape { ^~~~~ temp.cpp:62:7: error: redefinition of ‘class Ellipse’ class Ellipse : public Shape { ^~~~~~~ temp.cpp:19:8: note: previous definition of ‘class Ellipse’ struct Ellipse : public Shape { ^~~~~~~ temp.cpp:68:7: error: redefinition of ‘class Circle’ class Circle : public Ellipse { ^~~~~~ temp.cpp:27:8: note: previous definition of ‘class Circle’ struct Circle : public Ellipse { ^~~~~~ temp.cpp:74:7: error: redefinition of ‘class Rectangle’ class Rectangle : public Shape { ^~~~~~~~~ temp.cpp:33:8: note: previous definition of ‘class Rectangle’ struct Rectangle : public Shape { ^~~~~~~~~ temp.cpp:80:7: error: redefinition of ‘class Square’ class Square : public Rectangle { ^~~~~~ temp.cpp:41:8: note: previous definition of ‘class Square’ struct Square : public Rectangle { ^~~~~~ temp.cpp: In function ‘bool IsCircle(const Shape&)’: temp.cpp:87:18: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::CIRCLE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsEllipse(const Shape&)’: temp.cpp:91:18: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::ELLIPSE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsSquare(const Shape&)’: temp.cpp:95:18: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::SQUARE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsRectangle(const Shape&)’: temp.cpp:99:18: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::RECTANGLE; ^~~~~~~~~~~~

  2. Assistant

    Проблема в том, что у классов, которые наследуются от класса Shape, уже есть объявления структур со схожими названиями. Вам нужно заменить ключевое слово struct на class для определений классов Shape, Ellipse, Circle, Rectangle, Square. Кроме того, у класса Shape нужно добавить виртуальный деструктор, чтобы обеспечить правильное удаление объектов, у которых есть дочерние классы.

    Измененный код:

    #include

    enum class ShapeType { CIRCLE, ELLIPSE, SQUARE, RECTANGLE };

    class Shape { public: virtual ~Shape() {} virtual double area() = 0; virtual ShapeType GetShapeType() const = 0; };

    class Ellipse : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::ELLIPSE; } };

    class Circle : public Ellipse { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::CIRCLE; } };

    class Rectangle : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::RECTANGLE; } };

    class Square : public Rectangle { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::SQUARE; } };

    bool IsCircle(const Shape& shape) { return shape.GetShapeType() == ShapeType::CIRCLE; }

    bool IsEllipse(const Shape& shape) { return shape.GetShapeType() == ShapeType::ELLIPSE; }

    bool IsSquare(const Shape& shape) { return shape.GetShapeType() == ShapeType::SQUARE; }

    bool IsRectangle(const Shape& shape) { return shape.GetShapeType() == ShapeType::RECTANGLE; }

  3. User

    Задача: ShapesDetector Компилятор: g++17 7.3 controlled Вердикт: Ошибка при компиляции Статус: contest.submits.status.wrong

    stdout:

    stderr: temp.cpp:56:7: error: redefinition of ‘class Shape’ class Shape { ^~~~~ temp.cpp:15:8: note: previous definition of ‘class Shape’ struct Shape { ^~~~~ temp.cpp:63:7: error: redefinition of ‘class Ellipse’ class Ellipse : public Shape { ^~~~~~~ temp.cpp:19:8: note: previous definition of ‘class Ellipse’ struct Ellipse : public Shape { ^~~~~~~ temp.cpp:69:7: error: redefinition of ‘class Circle’ class Circle : public Ellipse { ^~~~~~ temp.cpp:27:8: note: previous definition of ‘class Circle’ struct Circle : public Ellipse { ^~~~~~ temp.cpp:75:7: error: redefinition of ‘class Rectangle’ class Rectangle : public Shape { ^~~~~~~~~ temp.cpp:33:8: note: previous definition of ‘class Rectangle’ struct Rectangle : public Shape { ^~~~~~~~~ temp.cpp:81:7: error: redefinition of ‘class Square’ class Square : public Rectangle { ^~~~~~ temp.cpp:41:8: note: previous definition of ‘class Square’ struct Square : public Rectangle { ^~~~~~ temp.cpp: In function ‘bool IsCircle(const Shape&)’: temp.cpp:88:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::CIRCLE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsEllipse(const Shape&)’: temp.cpp:92:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::ELLIPSE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsSquare(const Shape&)’: temp.cpp:96:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::SQUARE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsRectangle(const Shape&)’: temp.cpp:100:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::RECTANGLE; ^~~~~~~~~~~~

  4. Assistant

    Проблема в том, что у классов, которые наследуются от класса Shape, уже есть объявления классов со схожими названиями. Вам нужно удалить объявления классов Shape, Ellipse, Circle, Rectangle, Square, чтобы оказались только их определения.

    Измененный код:

    #include

    enum class ShapeType { CIRCLE, ELLIPSE, SQUARE, RECTANGLE };

    class Shape { public: virtual ~Shape() {} virtual double area() = 0; virtual ShapeType GetShapeType() const = 0; };

    class Ellipse : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::ELLIPSE; } };

    class Circle : public Ellipse { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::CIRCLE; } };

    class Rectangle : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::RECTANGLE; } };

    class Square : public Rectangle { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::SQUARE; } };

    bool IsCircle(const Shape& shape) { return shape.GetShapeType() == ShapeType::CIRCLE; }

    bool IsEllipse(const Shape& shape) { return shape.GetShapeType() == ShapeType::ELLIPSE; }

    bool IsSquare(const Shape& shape) { return shape.GetShapeType() == ShapeType::SQUARE; }

    bool IsRectangle(const Shape& shape) { return shape.GetShapeType() == ShapeType::RECTANGLE; }

  5. User

    stdout:

    stderr: temp.cpp:56:7: error: redefinition of ‘class Shape’ class Shape { ^~~~~ temp.cpp:15:8: note: previous definition of ‘class Shape’ struct Shape { ^~~~~ temp.cpp:63:7: error: redefinition of ‘class Ellipse’ class Ellipse : public Shape { ^~~~~~~ temp.cpp:19:8: note: previous definition of ‘class Ellipse’ struct Ellipse : public Shape { ^~~~~~~ temp.cpp:69:7: error: redefinition of ‘class Circle’ class Circle : public Ellipse { ^~~~~~ temp.cpp:27:8: note: previous definition of ‘class Circle’ struct Circle : public Ellipse { ^~~~~~ temp.cpp:75:7: error: redefinition of ‘class Rectangle’ class Rectangle : public Shape { ^~~~~~~~~ temp.cpp:33:8: note: previous definition of ‘class Rectangle’ struct Rectangle : public Shape { ^~~~~~~~~ temp.cpp:81:7: error: redefinition of ‘class Square’ class Square : public Rectangle { ^~~~~~ temp.cpp:41:8: note: previous definition of ‘class Square’ struct Square : public Rectangle { ^~~~~~ temp.cpp: In function ‘bool IsCircle(const Shape&)’: temp.cpp:88:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::CIRCLE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsEllipse(const Shape&)’: temp.cpp:92:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::ELLIPSE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsSquare(const Shape&)’: temp.cpp:96:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::SQUARE; ^~~~~~~~~~~~ temp.cpp: In function ‘bool IsRectangle(const Shape&)’: temp.cpp:100:14: error: ‘const class Shape’ has no member named ‘GetShapeType’ return shape.GetShapeType() == ShapeType::RECTANGLE; ^~~~~~~~~~~~

  6. Assistant

    Проблема в том, что у классов, которые наследуются от класса Shape, уже есть объявления структур со схожими названиями. Вам нужно заменить ключевое слово struct на class для определений классов Shape, Ellipse, Circle, Rectangle, Square. Кроме того, у класса Shape нужно добавить виртуальный деструктор, чтобы обеспечить правильное удаление объектов, у которых есть дочерние классы.

    Измененный код:

    #include

    enum class ShapeType { CIRCLE, ELLIPSE, SQUARE, RECTANGLE };

    class Shape { public: virtual ~Shape() {} virtual double area() = 0; virtual ShapeType GetShapeType() const = 0; };

    class Ellipse : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::ELLIPSE; } };

    class Circle : public Ellipse { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::CIRCLE; } };

    class Rectangle : public Shape { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::RECTANGLE; } };

    class Square : public Rectangle { public: double area() override; ShapeType GetShapeType() const override { return ShapeType::SQUARE; } };

    bool IsCircle(const Shape& shape) { return shape.GetShapeType() == ShapeType::CIRCLE; }

    bool IsEllipse(const Shape& shape) { return shape.GetShapeType() == ShapeType::ELLIPSE; }

    bool IsSquare(const Shape& shape) { return shape.GetShapeType() == ShapeType::SQUARE; }

    bool IsRectangle(const Shape& shape) { return shape.GetShapeType() == ShapeType::RECTANGLE; }