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 asint GetDetections
,virtual int GetDetections
.