我不知道如何解决的类错误消息

我有一个错误问题...我已经搜索了互联网,但没有找到解决该问题的真正方法。大多数人通过包含正确的头文件来解决它,但是我已经包含了所有我的文件。

我收到此错误消息:对`ShapeLib :: Circle ::〜Circle()的未定义引用。我不知道怎么了...

这是main.cpp的代码

#include <iostream>
#include <textwindow.h>
#include <circle.h>

using namespace ShapeLib;

void test(){
    TextWindow tw;

    Circle circle(5);

    tw.print(circle.getArea());
    tw.start();
}
int main(int argc, char *argv[]){

    test();

    return 0;
} 

circle.h代码:

#pragma once
#include "math.h"
#include "point.h"
#include <QPainter>

namespace ShapeLib{

    /**
    * A class representing a circle with a position in a two-dimensional
    * plane.
    */
    class Circle{

    public:

        /**
        * Creates a new circle with the radius 0 positioned at origin.
        */
        Circle();

        /**
        * Creates a new circle with the given radius positioned at
        * (centerX, centerY).
        */
        Circle(const double radius, const double centerX=0, const double centerY=0);

        /**
        * Returns this circle's radius.
        *
        * @return This circle's radius.
        */
        double getRadius() const;

        /**
        * Returns this circle's circumference.
        *
        * @return This circle's circumference.
        */
        double getCircumference() const;

        /**
        * Sets this circle's circumference to newCircumference.
        *
        * @parameter newCircumference This circles's new circumference.
        */
        void setCircumference(const double newCircumference);

        /**
        * Returns this circle's area.
        *
        * @return This circle's area.
        */
        double getArea() const;

        /**
        * Sets this circle's area to newArea.
        *
        * @parameter newArea This circle's new area.
        */
        void setArea(const double newArea);

        /**
        * Checks if point is located inside this circle.
        *
        * @parameter point The point to check for.
        * @return True if the point is inside this circle, otherwise false.
        */
        bool contains(const Point *point) const;

        /**
        * Moves this circle by dx in the x-direction and dy in the y-direction.
        *
        * @parameter dx The amount to move in the x-direction.
        * @parameter dy The amount to move in the y-direction.
        */
        void move(const double dx, const double dy);

        /**
        * Returns the x-coordinate for this circle's left side.
        *
        * @return The x-coordinate for this circle's left side.
        */
        double getLeft() const;

        /**
        * Returns the x-coordinate for this circle's right side.
        *
        * @return The x-coordinate for this circle's right side.
        */
        double getRight() const;

        /**
        * Returns the y-coordinate for this circle's top side.
        *
        * @return The y-coordinate for this circle's top side.
        */
        double getTop() const;

        /**
        * Returns the y-coordinate for this circle's bottom side.
        *
        * @return The y-coordinate for this circle's bottom side.
        */
        double getBottom() const;

        /**
        * Draws this circle with the provided background color and painter.
        */
        void draw(QPainter *painter, const Qt::GlobalColor color) const;

        ~Circle();

    private:
        double radius;
        Point *center; // Allocated dynamically for demonstrating destructors.

    };

}

circle.cpp代码

#include "circle.h"
#include "point.h"
#include "math.h"

using namespace ShapeLib;

Circle::Circle(){
    radius = 0;
    center = new Point(0, 0);
}

Circle::Circle(const double radius, const double centerX, const double centerY) : radius(radius) {
    center = new Point(centerX, centerY);
}

double Circle::getRadius() const {
    return radius;
}

double Circle::getCircumference() const {
    return 2*M_PI*radius;
}
void Circle::setCircumference(const double newCircumference) {
    radius = 2*M_PI*newCircumference;
}

double Circle::getArea() const {
    return M_PI*pow(radius, 2);
}
void Circle::setArea(const double newArea){
    radius = sqrt(newArea/M_PI);
}
评论
  • 本明远
    本明远 回复

    您声明了一个析构函数

    ~Circle();
    

    但从未在您的cpp文件中实现

    Circle::~Circle()
    {
        delete center;
    }
    

    Also beware the Rule of 5. As written, the default generated copy semantics of your class are broken, and would cause a double-deletion if copied. Therefore either make the class uncopyable or implement a user defined copy constructor and copy-assigment operator

    Circle(Circle const&) = delete;
    Circle& operator=(Circle const&) = delete;