对"vtable for "命名空间继承的未定义引用 对"类型信息"的未定义引用

undefined reference to `vtable for ' namespace inheritance undefined reference to `typeinfo for

本文关键字:引用 未定义 类型 信息 继承 vtable for 命名空间      更新时间:2023-10-16

我在编译(简单的)代码时具有这样的错误消息:

||=== Build: Release in zad12 (compiler: GNU GCC Compiler) ===|
obj/Release/Section.o||In function `MyFigures::Section::Section()':|
Section.cpp|| undefined reference to `vtable for MyFigures::Figure'|
obj/Release/Section.o||In function `MyFigures::Section::Section(Point, Point)':|
Section.cpp|| undefined reference to `vtable for MyFigures::Figure'|
obj/Release/Section.o||In function `MyFigures::Section::~Section()':|
Section.cpp|| undefined reference to `vtable for MyFigures::Figure'|
Section.cpp|| undefined reference to `vtable for MyFigures::Figure'|
obj/Release/Section.o:(.rodata._ZTIN10MyFigures7SectionE[_ZTIN10MyFigures7SectionE]+0x10)||undefined reference to `typeinfo for MyFigures::Figure'|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

和代码:

#ifndef _FIGURE_H_
#define _FIGURE_H_
namespace MyFigures
{
class Figure
{
    public:
        Figure(){}
         ~Figure(){}
        virtual void draw();
        virtual void move();
        virtual void scale(double s);
};
}
#endif
#ifndef _Section_H_
#define _Section_H_
#include "figure.h"
#include "point.h"
#include <exception>
namespace MyFigures
{
class Section : public Figure
{
public:
    class badsection : public std::exception
    {
    public:
        const char * what() const throw()
        {
            return "error";
        }
    };
    Section();
    Section(Point start, Point end);
    void draw();
    void move();
    void scale(double s);
    ~Section();
private:
    Point start;
    Point end;
};
}
#endif
#include "section.h"
namespace MyFigures
{
Section::Section()
{
}
Section::Section(Point start, Point end)
{
    if(start == end)
        throw badsection();
}
void Section::draw() {}
void Section::move() {}
void Section::scale(double s) {}
Section::~Section() {}
}
#ifndef _Point_H_
#define _Point_H_
class Point
{
    private:
        double x,y;
    public:
        Point();
        ~Point();
        Point(double xx);
        Point(double xx, double yy);
        Point& operator=(const Point& p);
        bool operator==(const Point& p);
};
#endif
#include "point.h"
Point::Point()
{
    x = 0;
    y = 0;
}
Point::Point(double xx)
{
    x = xx;
    y = 0;
}
Point::Point(double xx=0, double yy=0)
{
    x = xx;
    y = yy;
}
Point::~Point()
{
}
Point& Point::operator=(const Point& p)
{
    x = p.x;
    y = p.y;
    return *this;
}
bool Point::operator==(const Point& p)
{
    return (x == p.x) && (y == p.y);
}

我试图将Figure类中的破坏者作为虚拟,但没有效果(错误)。

问题是,您必须定义您声明的每个函数。这就是为什么编译器抱怨的原因,您没有为drawmovescale定义任何内容。

如果您希望Figure是一个完整的抽象类(我认为这是您要做的),则可以将这些功能设置为0,因此您不必定义它们,但是派生的类有实施它们。

//Now you don't need to implement them
virtual void draw() = 0;
virtual void move() = 0;
virtual void scale(double) = 0;