2016-03-19 2 views
-3

Ich bin nur ein Anfänger, der versucht, C++ zu lernen. Ich versuche das folgende Problem zu lösen. Ich habe keine Ahnung, was das Problem verursacht.Wie würde ich eine Unterklasse aus dieser abstrakten Klasse erstellen? Vtable Error

Dies ist die abstrakte Klasse I mit zur Verfügung gestellt werde:

#ifndef __EXPR_H__ 
#define __EXPR_H__ 
#include <string> 

class Expr { 
public: 
    virtual int eval() const = 0; 
    virtual std::string prettyPrint() const = 0; 
    virtual ~Expr(); 
}; 
#endif 

ich eine Unterklasse dieser Klasse zu erstellen versuchen, sieht meine .h-Datei wie diese

#ifndef __NUM_H__ 
#define __NUM_H__ 

#include <iostream> 
#include "expr.h" 

//class of lone expression 
class Num:public Expr 
{ 
private: 
    int operand; 
public: 
    Num(int operand):operand(operand){} 
    int eval() const; 
    std::string prettyPrint() const; 
    ~Num(){} 
}; 
#endif 

Während meiner Umsetzung Num Klasse sieht wie folgt aus: "num.cc"

#include "num.h" 
#include <sstream> 

std::string Num::prettyPrint() 
{ 
    std::stringstream convert; 
    convert << operand; 
    return convert.str(); 
} 

int Num::eval() 
{ 
    return operand; 
} 

Ich bekomme immer den folgenden Fehler. Ich habe keine Ahnung, was das verursacht.

Undefined symbols for architecture x86_64: 
    "vtable for Num", referenced from: 
     Num::Num(int) in rpn-dc20fb.o 
    NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. 
    "vtable for Expr", referenced from: 
     Expr::Expr() in rpn-dc20fb.o 
    NOTE: a missing vtable usually means the first non-inline virtual member function has no definition. 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

Jede Hilfe wäre willkommen. Vielen Dank!

Antwort

1

Virtuelle destructor benötigt eine Implementierung

virtual ~Expr() { } 

Der Compiler die virtuelle Tabelle eine virtuelle (rein oder nicht) destructor gegeben zu bauen versucht, und es beschwert sich, weil es nicht die Umsetzung finden.

1

Neben den Funktionen "eval" von P0W angegeben und "Schöndruck" als "const" in "num.cc" definiert werden müssen

std::string Num::prettyPrint() const 
{ 
    std::stringstream convert; 
    convert << operand; 
    return convert.str(); 
} 

int Num::eval() const 
{ 
    return operand; 
} 
Verwandte Themen