What I can see from your base class Element
is that this is an abstract type since at least one of its method's is declared as being purely virtual
. This means two things: first, you can not create an object of type Element
and second, all inherited classes must implement any functions that are declared as pure virtual
.
In your base class you have this as its declaration:
virtual float distance(Element const&, Element const&)=0;
and in your derived class you have this as its declaration:
float distance(Vector const&, Vector const&);//(?)
and in your derived class's implementation you have this:
float Vector::distance(Element const &a, Element const &b) {
return Vector::L2D(a,b);//Euclidean distance
}
The base class is stating that this pure virtual function that all derived class have to implement must return a float, and must accept two const references to an Element type. However in your inherited class its declaration is stating otherwise: it is stating that this function does return a float which is not an issue, but its parameter types are since it is being declared as taking two const references to a Vector type. If you are using MS Visual Studio 2012 or newer you can try doing this: add the keyword override
after the function declaration in the inherited class so that your inherited declaration would look like this:
float distance(Vector const&, Vector const&) override;
Then try to compile and see if you get any compiler, build or link errors.
The next step would be to change your derived class's declaration and definition to match the base class's declaration of the pure virtual signature. After doing this your derived function declaration would look like this:
float distance(Element const&, Element const&) override;
To understand this what is going on here; here is an example of what I have done to your original code so you can see what is happing with abstract types and inheritance.
Element.h
#ifndef ELEMENT_H
#define ELEMENT_H
class Element {
public:
virtual float distance( Element const&, Element const& ) = 0;
}; // Element
#endif // ELEMENT_H
Element.cpp
#include "stdafx.h"
#include "Element.h"
// ----------------------------------------------------------------------------
// distance()
float Element::distance( Element const&, Element const& ) {
return 0;
} // distance
Vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include "Element.h"
class Vector : public Element {
public:
float distance( Element const&, Element const& ) override;
}; // Vector
#endif // VECTOR_H
Vector.cpp
#include "stdafx.h"
#include "Vector.h"
// ----------------------------------------------------------------------------
// distance()
float Vector::distance( Element const&, Element const& ) {
return 1;
} // distance
main.cpp
#include "stdafx.h"
#include "Vector.h"
int main() {
float val = 0;
Vector v1;
Vector v2;
Vector v3;
val = v3.distance( v1, v2 );
std::cout << val << std::endl;
std::cout << "Press any key to quit" << std::endl;
_getch();
return 0;
} // main
It is hard to tell exactly what you are asking and what you are trying to achieve, but from looking at your code alone, it appears you are using an abstract type using inheritance, but your inherited member function signature is wrong as it does not match the base's pure virtual method's signature.
If you look closely at the small program that I presented to you; this compiles, builds and executes and I am getting the appropriate results.
If you notice here, the declaration is expecting two const&
to a type of an Element
object, yet when I call the method I am passing in two declared variable types or instances of a Vector
object. This works because the Vector
objects are inherited from an Element
type. Also if you look at the output value, the value printed is 1 and not 0. This is what you want when you are trying to override
a base class's function declaration.
Element::distance()
returns 0 - This can not be called since it is a pure virtual method causing the base class to be abstract
Vector::distance()
returns 1 - This will be called on a vector object.
Maybe this will help you to achieve what you are after.