My base class:
//Element.h
class Element
{
public:
Element();
virtual ~Element(){}; // not sure if I need this
virtual Element& plus(const Element&);
virtual Element& minus(const Element&);
};
Derived template class:
//Vector.h
#include "Element.h"
template <class T>
class Vector: public Element {
T x, y, z;
public:
//constructors
Vector();
Vector(const T& x, const T& y = 0, const T& z =0);
Vector(const Vector& u);
...
//operations
Element& plus(const Element&) const;
Element& minus(const Element&) const;
...
};
...
//summation
template <class T>
Element& Vector<T>::plus(const Element& v) const
{
const Vector<T>& w = static_cast<const Vector<T>&>(v);
Vector<T>* ret = new Vector<T>((x + w.x), (y + w.y), (z + w.z));
return *ret;
}
//difference
template <class T>
Element& Vector<T>::minus(const Element& v) const
{
const Vector<T>& w = static_cast<const Vector<T>&>(v);
Vector<T>* ret = new Vector<T>((x - w.x), (y - w.y), (z - w.z));
return *ret;
}
I had another issue with this code (answered in another post), but currently I'm wrestling with the fact that if I try to run this, I get
Undefined symbols: "Element::plus(Element const&)", referenced from:
vtable for Vectorin main.o
"Element::Element()", referenced from:
Vector::Vector()in main.o
Vector::Vector(double const&, double const&, double const&)in main.o
"Element::minus(Element const&)", referenced from:
vtable for Vectorin main.o
"typeinfo for Element", referenced from:
typeinfo for Vectorin main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Is this because the derived template class isn't housed in the same file as the base class, and that's causing compiler issues (similar to how I had to define the entire Vector class in the header file)?
I'm fairly new to C++, and still reading up on what vtables are and how they work, but I can't quite figure this out yet.