C++ 使用虚拟析构函数会产生错误
我正在为学校做一个小节目。我想将虚拟析构函数添加到类“IObject”、“Square”和“RotatedSquare”。但当我这样做的时候,它给出了一个错误。在这种情况下是不可能的吗?或者在这里怎么做?这是我的密码: 接口对象:C++ 使用虚拟析构函数会产生错误,c++,interface,virtual,destructor,C++,Interface,Virtual,Destructor,我正在为学校做一个小节目。我想将虚拟析构函数添加到类“IObject”、“Square”和“RotatedSquare”。但当我这样做的时候,它给出了一个错误。在这种情况下是不可能的吗?或者在这里怎么做?这是我的密码: 接口对象: #ifndef IOBJECT_H_ #define IOBJECT_H_ #include "Point.h" class IObject { public: virtual const Point& getPosition()const = 0;
#ifndef IOBJECT_H_
#define IOBJECT_H_
#include "Point.h"
class IObject {
public:
virtual const Point& getPosition()const = 0;
virtual double getSize()const = 0;
//virtual ~IObject();
};
#endif
阶级广场
#ifndef SQUARE_H_
#define SQUARE_H_
#include "IObject.h"
#include "Point.h"
#include <iostream>
class Square : public IObject{
private:
Point position;
double size;
public:
Square(const Point& position, const double size):position(position),size(size){};
virtual const Point& getPosition() const {return this->position;}
virtual double getSize() const {return this->size;}
friend std::ostream& operator<<(std::ostream& out, const Square& s);
//virtual ~Square();
protected:
virtual void print(std::ostream& out) const;
};
#endif
错误:
Undefined symbols for architecture x86_64:
"Square::~Square()", referenced from:
_main in Practice.o
"vtable for RotatedSquare", referenced from:
RotatedSquare::RotatedSquare(Point const&, double, double) in Practice.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for Square", referenced from:
Square::Square(Point const&, double) in Practice.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for IObject", referenced from:
IObject::IObject() in Practice.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
主要类别:
#include <iostream>
#include "Point.h"
#include "IObject.h"
#include "Square.h"
#include "RotatedSquare.h"
int main() {
IObject* s1 = new Square(Point(1,4),2.2);
std::cout << s1->getPosition() << std::endl;
std::cout << s1->getSize() << std::endl;
Square s2 = Square(Point(4,5),5);
Square* s3 = new RotatedSquare(Point(5,5),8,60);
std::cout << s2 << std::endl;
std::cout << *s3;
return 0;
}
#包括
#包括“h点”
#包括“IObject.h”
#包括“Square.h”
#包括“旋转正方形.h”
int main(){
IObject*s1=新的正方形(点(1,4),2.2);
std::cout getPosition()您需要定义您声明的每个虚拟析构函数。如果您不希望析构函数执行默认行为以外的操作,则可以使用声明进行定义:
而不是:
virtual ~IObject();
使用
如果您希望在析构函数中执行重要的工作,或者通常更喜欢cpp中的所有定义而不是hpp中的所有定义,那么请在cpp文件中定义析构函数(在hpp中保留声明而不定义)
您需要在代码中定义虚拟析构函数。
正如你的错误中明确提到的
"Square::~Square()", referenced from:
_main in Practice.o
"vtable for RotatedSquare", referenced from:
RotatedSquare::RotatedSquare(Point const&, double, double) in Practice.o
注意:缺少vtable通常意味着第一个非内联虚拟成员
函数没有定义。
编写virtual~IObject(){}
就足够了。您的析构函数缺少定义
如果您不想提供自己的,只需使用编译器生成的:
virtual ~X() = default;
这比提供空实体要好:
virtual ~X() { }
因为用户提供的1析构函数阻止类被删除
1{/*anything*/}
让它由用户提供,=default
不提供。注意什么错误?[OT]:删除
缺失(或者更好:额外的新增
存在:使用智能指针)谢谢!你知道为什么我不能删除析构函数中的双份吗?C++说:不能删除“double”类型的表达式是指针所拥有的对象。给定的删除表达式类型必须是指针。如果你分配了一个double,带有代码。< < /代码> >它是正确的<代码>删除< /代码>它,你会用一个指向该double的指针的表达式,而不是double本身的表达式。如果double是其他对象的一部分,或者是局部变量,或者没有直接分配给new
,则删除它是不正确的。
"Square::~Square()", referenced from:
_main in Practice.o
"vtable for RotatedSquare", referenced from:
RotatedSquare::RotatedSquare(Point const&, double, double) in Practice.o
virtual ~X() = default;
virtual ~X() { }