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

时间:2020-05-11 15:20:14

标签: c++ class

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

我收到此错误消息:对“ 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);
}

1 个答案:

答案 0 :(得分:1)

您声明了析构函数

~Circle();

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

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

还请注意Rule of 5。如所写,类的默认生成的复制语义已损坏,并且如果复制,将导致双重删除。因此,要么使该类不可复制,要么实现一个用户定义的复制构造函数和复制辅助运算符

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