0

I am trying to do an OOP simulation of using a bank Card, just for practice, nothing too complex; I'm getting a strange error. The call is a little bit silly, but i was just trying to see if "read" function works ok, not to test it's real purpose. I don't understand why I get the error below, i know that i can acces a virtual method with an object directly, not necessarily with a pointer. Also, sorry for most of the members not being written in english. Here is my code:

#include <iostream>
#include <string>
using namespace std;

class Card
{

protected:
    string nrCard;
    string NumeDetinator;
    string data_expirare;
    int CVV;
    double credit;

    static unsigned nr_ord;

public:
    Card();
    Card(string nC, string ND, string d_ex, int cvv, double CRD);
    virtual getCredit(); //= 0;
    virtual void print(ostream&) const;//= 0;
    virtual void read(istream&) ;//= 0;
    virtual void print(ostream &) ;//= 0;
};

unsigned Card :: nr_ord = 0;

Card :: Card() : nrCard("XXXX - XXXX - XXXX - XXXX"), NumeDetinator("???? ????"), data_expirare("DD - MM - YY"), CVV(999), credit(0.0) {}
Card :: Card(string nC, string ND, string d_ex, int cvv, double CRD) : nrCard(nC), NumeDetinator(ND), data_expirare(d_ex), CVV(cvv), credit(CRD) {}

void Card::print(ostream& out) const {
    out << "numar card: " << this->nrCard << "\n";
    out << "Nume detinator: " << this->NumeDetinator << "\n";
    out << "data expirare: " << this->data_expirare << "\n";
    out << "CVV " << this->CVV << "\n";
    out << "Credit disponibil: " << this->credit << "\n";
}

void Card::read(istream& in) {
    cout << "Dati datele cardului:\n numar card: ";
    in >> this->nrCard;
    cout << "\nNumele detinatorului: ";
    in >> this->NumeDetinator;
    cout << "\nData expirarii: ";
    in >> this->data_expirare;
    cout << "\nCVV: ";
    in >> this->CVV;
    cout<< "\ncredit: ";
    in >> this->credit;
}

class Card_Standard : Card
{
    int limitaExtragere;
    double comisionDepasireLimita;
};

class Card_Premium : Card_Standard
{
  int m;
};


int main() {

Card C;

C.print(cout);  /// error: undefined reference to vtable for Card , id returned 1 exit status;


return 0;
}


  • 1
    FWIW, don't store money as a `double`. floating point math is imprecise and will cause rounding errors. Instead, use a 64 bit wide integer type, and store the value in there as cents or a power of ten of a cent. This gives you integer math with is exact, and is easy to convert to dollars and cents when needed to be displayed. – NathanOliver Apr 08 '21 at 19:53
  • Yeah. People get really weird when it comes to money in the real world. Even though it's the same bug, they only seem to get mad when their money rounds down. Rounds up they have no problem with. Stupid, inconsistent humans. – user4581301 Apr 08 '21 at 20:02
  • You are missing non-const version of `print`. – ALX23z Apr 08 '21 at 20:05

1 Answers1

1

"undefined reference to vtable" is compiler's way of saying "not all virtual methods are implemented".

Implement all virtual members (or mark them pure) and the error will go away.

BTW, virtual getCredit(); is missing a return type (probably double?).

rustyx
  • 80,671
  • 25
  • 200
  • 267