【问题标题】:undefined reference to `vtable for ' namespace inheritance undefined reference to `typeinfo for [duplicate]未定义对“vtable for”命名空间继承的引用未定义对“typeinfo”的引用[重复]
【发布时间】:2016-10-19 14:43:50
【问题描述】:

编译我的(简单)代码时出现这样的错误消息:

||=== 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 类中的析构函数设为虚拟,但没有效果(错误仍然存​​在)。

【问题讨论】:

  • vtable 放置在定义第一个虚函数的位置,但您似乎从未从基类中定义函数。

标签: c++


【解决方案1】:

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

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

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

【讨论】:

  • 就是这样!非常感谢 - 我完全忘记了这一点。
  • @user2079303 我总是搞混,谢谢:)
猜你喜欢
  • 1970-01-01
  • 2012-03-13
  • 2015-04-08
  • 2014-09-24
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 2011-12-01
相关资源
最近更新 更多