基本クラスにGetDetections
は、文字列ファイル名を受け取り、機能セットを構築し、その作業を純粋仮想関数に任せる関数がありますGetDetections
。
サブクラスでは、この仮想関数を実装します。
ではmain
、サブクラスのインスタンスがありGetDetections
、ファイル名で呼び出します。これは、文字列引数を受け入れる基本クラスの非仮想関数を呼び出すと思いましたが、これはコンパイルされません。エラーは次のとおりです。
prog.cpp: 関数 'int main()' 内:
prog.cpp:33:48: エラー: 'SubClass::GetDetections(const char [13])' の呼び出しに一致する関数がありません</p>
prog.cpp:33:48: 注: 候補は:
prog.cpp:26:9: 注: virtual int SubClass::GetDetections(const std::vector&) const prog.cpp:26:9: 注: 'const char [13]' から ' への引数 1 の既知の変換はありませんconst std::vector&'</p>
これがコードです。( http://ideone.com/85Afyxにも掲載)
#include <iostream>
#include <string>
#include <vector>
struct Feature {
float x;
float y;
float value;
};
class BaseClass {
public:
int GetDetections(const std::string& filename) const {
// Normally, I'd read in features from a file, but for this online
// example, I'll just construct an feature set manually.
std::vector<Feature> features;
return GetDetections(features);
};
// Pure virtual function.
virtual int GetDetections(const std::vector<Feature>& features) const = 0;
};
class SubClass : public BaseClass {
public:
// Giving the pure virtual function an implementation in this class.
int GetDetections(const std::vector<Feature>& features) const {
return 7;
}
};
int main() {
SubClass s;
std::cout << s.GetDetections("testfile.txt");
}
私はもう試した:
GetDetections
サブクラスで , としてint GetDetections
宣言しvirtual int GetDetections
ます。