[英]Class error message that I don't know how to solve
我有一個錯誤問題...我已經搜索了互聯網,但沒有找到真正解決問題的方法。 大多數人通過包含正確的 header 文件來解決它,但我已經包含了所有我的文件。
我收到此錯誤消息:未定義對“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;
}
還要注意5 規則。 如所寫,您的 class 的默認生成復制語義已損壞,如果復制會導致雙重刪除。 因此,要么使 class 不可復制,要么實現用戶定義的復制構造函數和復制分配運算符
Circle(Circle const&) = delete;
Circle& operator=(Circle const&) = delete;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.