构造函数/解构函数情况下的'undefined reference to vtable for'
'undefined reference to vtable for' in Case of Constructor/Desconstructor
我得到了两个错误"对vtable的未定义引用…"当我在clang++中使用带有发布模式的c++11练习多态性时。下面是我的代码的一些部分。
#include <armadillo>
typedef float CHOSENONE;
typedef std::vector<std::vector<CHOSENONE> > fVect2d;
typedef std::vector<CHOSENONE> fVect;
class CfSubSet {
private:
fmat m_Data;
frowvec m_Label;
int m_NumInstances, m_NumFeatures ;
public:
CfSubSet(){
cout<<"hello! n" <<endl;
m_NumInstances = 0;
m_NumFeatures = 0;
m_Data.zeros();
m_Label.zeros();
}
CfSubSet(int iNumInstances, int iNumFeatures):m_NumInstances(iNumInstances),m_NumFeatures(iNumFeatures){
m_Data.resize(iNumInstances,iNumFeatures);
m_Label.resize(iNumInstances);
}
~CfSubSet(){
m_Data.reset();
m_Label.reset();
}
inline int getNumInstances(){return m_NumInstances;};
inline int getNumFeatures(){return m_NumFeatures;};
inline fmat& getData(){return m_Data;};
inline fmat& getLabel(){return m_Label;};
};
class CData
{
protected:
CfSubSet * m_Test;
CfSubSet * m_Train;
public:
CData(){
m_Test = new CfSubSet();
m_Train = new CfSubSet();
};
~CData(){
delete m_Test;
delete m_Train;
};
virtual void readDataFile(string ifFileTrainName) = 0;
virtual void readLabelFile(string ifFileLabelFile) = 0;
virtual void readTestFile(string ifFileTestFile) = 0;
virtual void readGroundTruthFile(string ifGroundTruthFile) = 0;
};
class CMNISTSet : public CData
{
public:
CMNISTSet() {};
~CMNISTSet(){};
void readDataFile(string ifFileTrainName);
void readLabelFile(string ifFileLabelFile);
void readTestFile(string ifFileTestFile);
void readGroundTruthFile(string ifGroundTruthFile);
private:
void ReadData(string ifFileName, fmat & rfOut);
void ReadLabel(string ifFileName, frowvec &rfOut);
int reverseInt (int i);
};
根据vtable 的未定义参考
解决方案是确保所有非纯的虚拟方法定义。请注意,必须定义析构函数,即使它是声明的纯虚拟
我已经实现了所有的虚拟函数,但在析构函数的情况下,我没有理解这一点,因为我也定义了。我是否遗漏了代码中必须包含的内容?有人能帮忙吗。提前谢谢。
UPDATE 1:我把完整的代码放在这里。在MNISTSet.cpp中
#include "MNISTSet.h"
int CMNISTSet::reverseInt (int i){
unsigned char c1, c2, c3, c4;
c1 = i & 255;
c2 = (i >> 8) & 255;
c3 = (i >> 16) & 255;
c4 = (i >> 24) & 255;
return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
void CMNISTSet::ReadData(string ifFileName, fmat & rfOut){
ifstream ifs(ifFileName.c_str(),std::ios::in | std::ios::binary);
int magic_number = 0;
int number_of_images = 0;
int rows = 0;
int cols = 0;
ifs.read((char*)&magic_number,sizeof(magic_number));
magic_number= reverseInt(magic_number);
ifs.read((char*)&number_of_images,sizeof(number_of_images));
number_of_images= reverseInt(number_of_images);
ifs.read((char*)&rows,sizeof(rows));
rows= reverseInt(rows);
ifs.read((char*)&cols,sizeof(cols));
cols= reverseInt(cols);
rfOut = fmat(number_of_images, rows*cols);
cout << magic_number << " " << number_of_images << " " << rows << " " << cols << endl;
for(int i = 0; i < number_of_images; i++){
for(int row = 0; row < rows; row++){
for(int col = 0; col < cols; col++){
unsigned char temp = 0;
ifs.read((char*)&temp,sizeof(temp));
rfOut(i,rows*row+col) = (CHOSENONE)temp;
}
}
}
}
void CMNISTSet::readDataFile(string ifFileTrainName){
ReadData(ifFileTrainName, this.m_Train->getData());
}
void CMNISTSet::readTestFile(string ifFileTestFile){
ReadData(ifFileTestFile, this.m_Test->getData());
}
void CMNISTSet::ReadLabel(string ifFileName, frowvec & rfOut){
ifstream ifs(ifFileName.c_str(),std::ios::in | std::ios::binary);
int magic_number = 0;
int number_of_images = 0;
ifs.read((char*)&magic_number,sizeof(magic_number));
magic_number= reverseInt(magic_number);
ifs.read((char*)&number_of_images,sizeof(number_of_images));
number_of_images= reverseInt(number_of_images);
rfOut = frowvec(number_of_images);
cout << number_of_images << endl;
for(int i = 0; i < number_of_images; i++){
unsigned char temp = 0;
ifs.read((char*)&temp,sizeof(temp));
rfOut(i) = (CHOSENONE)temp;
}
}
void CMNISTSet::readLabelFile(string ifFileTestFile){
ReadLabel(ifFileTestFile, m_Labels);
}
void CMNISTSet::readGroundTruthFile(string ifGroundTruthFile){
ReadLabel(ifGroundTruthFile, m_GroundTruth);
}
必须在某个地方定义子类函数声明。
为了测试,我将您的覆盖声明更改为:
void readDataFile(string ifFileTrainName) {}
void readLabelFile(string ifFileLabelFile) {}
void readTestFile(string ifFileTestFile) {}
void readGroundTruthFile(string ifGroundTruthFile) {}
这是有效的。
如果在单独的.cpp文件中定义函数。然后您需要指定它们的类。
void CMNISTSet::readDataFile( ... ){ ... }
...
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- 安卓 NDK 制造。数百"undefined reference error"
- 当我尝试进行TPC-E测试时,实用程序抱怨"undefined reference"
- 声明和定义函数静态会产生"undefined reference to function_name()"
- C++ : "undefined reference to"可能是因为CMake文件
- 包含<iostream>时"Undefined reference"错误
- CMake 'undefined reference' CERN-ROOT 的错误
- 生成错误:"cannot find target for file"和"undefined reference"
- AppData\Local\Temp\cc59LXDc.o:test1.cpp: undefined reference to 'constructNewObjectOfClass
- YAML-cpp 编译错误:"undefined reference to YAML::LoadFile"
- 添加 cpp11 插件时出现错误消息"Undefined reference to boost (...)"