我正在尝试为我正在学习的类(class)构建一个 C++
项目,但我遇到了很多麻烦。
我有这个标题:
#ifndef COMPTE_H_
#define COMPTE_H_
#include <string>
class Compte {
public:
Compte (unsigned int p_noCompte, double p_tauxInteret, double p_solde,const std::string& p_description);
virtual ~Compte (){} ;
void asgSolde (const double p_solde);
unsigned int reqNoCompte () const;
double reqTauxInteret () const;
double reqSolde () const;
std::string reqDescription () const;
std::string reqCompteFormate() const;
virtual Compte* clone() const;
virtual const double calculerInteret();
private:
unsigned int m_noCompte;
double m_tauxInteret;
double m_solde;
std::string m_description;
};
#endif /* COMPTE_H_ */
以及对应的cpp文件:
#include "Compte.h"
#include <string>
#include <sstream>
using namespace std;
Compte::Compte (unsigned int p_noCompte, double p_tauxInteret, double p_solde, const string& p_description)
: m_noCompte(p_noCompte), m_tauxInteret(p_tauxInteret), m_solde(p_solde), m_description(p_description)
{
}
void Compte::asgSolde (const double p_solde)
{
m_solde = p_solde;
}
unsigned int Compte::reqNoCompte () const{
return m_noCompte;
}
double Compte::reqTauxInteret() const{
return m_tauxInteret;
}
double Compte::reqSolde() const{
return m_solde;
}
string Compte::reqDescription() const{
return m_description;
}
string Compte::reqCompteFormate()const
{
ostringstream compteFormate;
return compteFormate.str();
}
但是我弹出以下错误:
Description Resource Path Location Type
undefined reference to « vtable for Compte » Compte.cpp /Travail Pratique 2 line 14 C/C++ Problem
对于.cpp文件中的构造函数,
Description Resource Path Location Type
undefined reference to « vtable for Compte » Compte.cpp /Travail Pratique 2 line 14 C/C++ Problem
对于 .header 文件中的 class Compte{
行,最后
Description Resource Path Location Type
undefined reference to « vtable for Compte » Compte.h /Travail Pratique 2 line 16 C/C++ Problem
对于 virtual ~Compte(){};
行。
我的代码有什么问题,我该如何更正?
最佳答案
您忘记实现 2 个虚拟方法,clone 和 calculerInteret。 这就是您的链接器提示的原因。您的链接器没有提示析构函数,但他在创建虚拟方法表时遇到了麻烦,因为缺少 2 个标记为虚拟的方法。 只有链接器才能发现这样的问题,因为理论上这些方法甚至可以分布在多个源文件中。
如果您打算创建抽象方法,请执行以下操作:
virtual Compte* clone() const=0;
virtual const double calculerInteret()=0;
当然你知道你不能实例化具有抽象方法的类吧?
关于c++ - 未定义对 Compte 的 vtable 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29175026/