c++ - 对 `vtable for ' 命名空间继承的 undefined reference 对 `typeinfo 的 undefined reference

标签 c++

<分区>

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

||=== 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;

关于c++ - 对 `vtable for ' 命名空间继承的 undefined reference 对 `typeinfo 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37887024/

相关文章:

c++ - 读取二进制文件的迭代器

c++ - vector 中的“one-past-the-last-element”是什么意思?

c++ - ISO/IEC 网站和 C 和 C++ 标准收费

c++ - 如果双重注册,如何从 boost 树中删除 child ?

c++ - 如何返回 vector 的const引用?

c++ - 如何在opencv c++中对图像进行骨架化

c++ - 我无法正确声明这些变量...为什么这些参数不起作用?我只是将 n 值设置为数组长度

c++ - 我可以将 OpenCV 的发布配置与我的应用程序的调试配置一起使用吗?

c++ - 满时分配一个新数组?

c++ - std::find 不适用于 istream_iterators