为什么我不能通过对象调用访问虚函数?
Why can't I acces the virtual function through an object call?
我正在尝试做一个使用银行卡的OOP模拟,只是为了练习,没有什么太复杂的;我收到一个奇怪的错误。这个调用有点傻,但我只是想看看“读取”功能是否正常工作,而不是测试它的真正目的。我不明白为什么会出现以下错误,我知道我可以直接使用对象访问虚方法,而不必使用指针。另外,很抱歉大多数成员不是用英语写的。这是我的代码:
#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;
}
"对 vtable 的未定义引用" 是编译器的说法"并非所有虚拟方法都已实现"。
实施所有 virtual
成员(或将它们标记为纯),错误将消失。
顺便说一句,virtual getCredit();
缺少 return 类型(可能是 double
?)。
我正在尝试做一个使用银行卡的OOP模拟,只是为了练习,没有什么太复杂的;我收到一个奇怪的错误。这个调用有点傻,但我只是想看看“读取”功能是否正常工作,而不是测试它的真正目的。我不明白为什么会出现以下错误,我知道我可以直接使用对象访问虚方法,而不必使用指针。另外,很抱歉大多数成员不是用英语写的。这是我的代码:
#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;
}
"对 vtable 的未定义引用" 是编译器的说法"并非所有虚拟方法都已实现"。
实施所有 virtual
成员(或将它们标记为纯),错误将消失。
顺便说一句,virtual getCredit();
缺少 return 类型(可能是 double
?)。