使用虚析构函数会产生错误
Using virtual destructors gives error
我在为学校编写一个小程序。我想添加虚拟析构函数类'IObject', 'Square'和'RotatedSquare'。但当我这样做时,它会给出一个错误。在这种情况下是不可能的还是在这里怎么做?下面是我的代码:
接口对象:
#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
类RotatedSquare #ifndef ROTATEDSQUARE_H_
#define ROTATEDSQUARE_H_
#include "Square.h"
class RotatedSquare : public Square{
private:
double angle;
public:
RotatedSquare(const Point& position, const double size, const double angle): Square(position,size),angle(angle){};
double getAngle() const {return this->angle;}
//virtual ~RotatedSquare();
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;
}
您需要定义您声明的每个虚析构函数。假设你不希望析构函数做比默认行为更多的事情,你可以通过声明来定义:而不是:
virtual ~IObject();
使用virtual ~IObject() {}
如果你想在析构函数中做重要的工作,或者通常更喜欢所有的定义都在cpp而不是hpp中,那么在cpp文件中定义析构函数(在hpp中保留没有定义的声明)
IObject::~IObject()
{
// Whatever needs to be done
}
您需要在代码中定义虚析构函数。
"Square::~Square()", referenced from:
_main in Practice.o
"vtable for RotatedSquare", referenced from:
RotatedSquare::RotatedSquare(Point const&, double, double) in Practice.o
注意:虚函数表缺失通常是指第一个非内联虚成员函数没有定义
写virtual ~IObject() {}
就够了
你的析构函数缺少定义。
如果你不想提供你自己的,你可以简单地使用编译器生成的:
virtual ~X() = default;
这比提供一个空体要好:
virtual ~X() { }
,因为用户提供的1析构函数阻止了类平凡的可析构。
1 { /* anything */ }
由用户提供,= default
不由用户提供。
相关文章:
- 不命名构造函数和析构函数上的类型错误
- 导致 Seg 错误的 SingleLinkedList 的析构函数
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 矢量擦除调用的错误析构函数
- 在析构函数中调用"delete"运算符时"compiler is out of heap space"编译器错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 导致堆缓冲区错误的动态分配数组的析构函数
- 非默认析构函数会导致不完整的类型错误
- 析构函数删除错误的元素
- 使用自定义工具集获取动态退出析构函数链接错误 - eh 矢量析构函数
- 使用 boost::thread 运行函子,但析构函数被错误地调用
- 错误 c++ 在令牌之前'('预期的构造函数、析构函数或类型转换
- 错误:在“(”标记之前进行预期的构造函数、析构函数或类型转换.即使我有一个构造函数
- 模板类中的析构函数给出错误
- 在析构函数内取消引用指针时出现分段错误
- 模板化子类析构函数中的隔离错误
- GCC 9.1 返回 void& 作为显式析构函数调用的结果类型。这是一个错误吗?
- 获取错误:在“(”标记之前进行预期的构造函数、析构函数或类型转换
- 显式调用类的析构函数时出现错误
- C++析构函数调用了错误的对象