0

In the base class, I have a function GetDetections which takes a string filename, constructs a feature set, and defers its work to a pure virtual function GetDetections.

In the subclass, I implement this virtual function.

In main, I have an instance of the subclass, and call GetDetections with a filename. I thought this would call the base class's non-virtual function that accepts a string argument, but this does not compile. The error is:

prog.cpp: In function ‘int main()’:

prog.cpp:33:48: error: no matching function for call to ‘SubClass::GetDetections(const char [13])’

prog.cpp:33:48: note: candidate is:

prog.cpp:26:9: note: virtual int SubClass::GetDetections(const std::vector&) const prog.cpp:26:9: note: no known conversion for argument 1 from ‘const char [13]’ to ‘const std::vector&’

Here is the code. (Also posted at 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");
}

I've tried:

  • Declaring GetDetections in the subclass as int GetDetections, virtual int GetDetections.
  • Related: http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the, http://stackoverflow.com/questions/411103/function-with-same-name-but-different-signature-in-derived-class –  Apr 15 '13 at 06:17

2 Answers2

3

The implementation of the virtual function inside derived class with hide the overloaded GetDirections function in base class.

try this:

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

for SubClass

taocp
  • 23,276
  • 10
  • 49
  • 62
  • That worked (http://ideone.com/rEW7Dt) Thank you! However, this seems cumbersome. Is this the standard way to do this? –  Apr 15 '13 at 00:28
  • 1
    @Sancho I think so. There should be another way, but I just cannot remember for now. – taocp Apr 15 '13 at 00:29
  • 1
    Yes, a using declaration like this is the best way to unhide members. – aschepler Apr 15 '13 at 00:34
0

Overloaded virtual functions are hidden. However you can call the BaseClasse's GetDetections with its qualified name as

std::cout << s.BaseClass::GetDetections("testfile.txt");
stardust
  • 5,918
  • 1
  • 18
  • 20