0

I have such error messages while compiling my (simple) code:

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

And the code:

#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);
}

I tried to make the destructor in Figure class as virtual, but no effect (errors remain).

Brian Brown
  • 3,873
  • 16
  • 48
  • 79
  • The vtable is placed where the first virtual function is defined, but you seem to never define the functions from the base class. – Bo Persson Jun 17 '16 at 17:23

1 Answers1

1

The problem is, that you have to define every function you declare. That's why the compiler is complaining, you are not defining anything for draw, move or scale.

If you want Figure to be a complete abstract class (which I think this is what you are trying to do), you can set those function to 0, so you don't have to define them, but the derived classes have to implement them.

//Now you don't need to implement them
virtual void draw() = 0;
virtual void move() = 0;
virtual void scale(double) = 0;
Rakete1111
  • 47,013
  • 16
  • 123
  • 162