0
我對C++相當陌生,而且我似乎無法找到任何與我具有完全相同問題的人。基本上,我想要一個我從未直接實例化的抽象類,以及幾個子類。此外,我試圖保持所有超級/子類的一致模板。這是我的源文件。我有3個實用程序文件和一個用於主函數的.cpp文件。找不到方法:模板,虛擬方法,繼承,多態
abstract_matrix.h
#ifndef ABSTRACTMATRIX
#define ABSTRACTMATRIX
template<class T>
class DataMatrix {
public:
int numFeatures;
int numPoints;
T* data;
T* classifications;
virtual void scale(T scalar) = 0;
};
#endif
這裏是一個抽象類的子類,我聲明,sparse_host_matrix.h
#ifndef SPARSEHOSTMATRIX
#define SPARSEHOSTMATRIX
#include <iostream>
template<class T>
class SparseHostMatrix : public DataMatrix<T> {
public:
void scale(T scalar);
};
#endif
下面是這些功能的實現..
#include "sparse_host_matrix.h"
#include <iostream>
template<class T>
void SparseHostMatrix<T>::loadFromFile(char* filename) {
std::cout << "Loading in sparseHostMatrix" << std::endl;
}
template<class T>
void SparseHostMatrix<T>::scale(T scalar) {
std::cout << "Loading in sparseHostMatrix" << std::endl;
}
而且當我運行這個主要功能...
#include <iostream>
using namespace std;
#include "abstract_matrix.h"
#include "sparse_host_matrix.h"
int main() {
DataMatrix<double> *myMat = new SparseHostMatrix<double>;
myMat->scale(.5);
}
我得到的錯誤未定義的參考`SparseHostMatrix ::規模(雙)
對不起,我的代碼巨量,我只是很困惑,並一直停留在這一段時間沒有成功在SO或其他方面找到解決方案。