2016-06-17 46 views
0

我有這樣的錯誤消息,在編譯時我的(簡單)的代碼:未定義的參考`虛表的'命名空間繼承未定義的參考`所屬類別爲

||=== 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類中的析構函數成爲虛擬的,但沒有效果(錯誤仍然存​​在)。

+0

vtable被放置在第一個虛函數被定義的地方,但你似乎從來沒有定義基類的函數。 –

回答

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; 
+0

就是這樣!非常感謝你 - 我完全忘記了這一點。 –

+0

@ user2079303我總是把這些混在一起,謝謝:) – Rakete1111