0

基本クラスに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ます。
4

2 に答える 2

3

派生クラス内の仮想関数の実装は、GetDirections基本クラスのオーバーロードされた関数を非表示にします。

これを試して:

using BaseClass::GetDetections;
int GetDetections(const std::vector<Feature>& features) const {
    return 7;
}

サブクラス用

于 2013-04-15T00:26:33.493 に答える
0

オーバーロードされた仮想関数は非表示になります。ただし、 の修飾名を次BaseClasseのように呼び出すことができます。GetDetections

std::cout << s.BaseClass::GetDetections("testfile.txt");
于 2013-04-15T00:24:30.530 に答える