Calling a function from a template derived class

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 problem with this code (answered in another post ), but I'm currently struggling with that if I try to run this, I get

Undefined :   ":: ( const amp;)", :
     vtable Vectorin main.o
  ":: ()", :
     :: () main.o
     Vector:: Vector (double const &, double const &, double const &) main.o
 :: ( const &) ", :
     vtable Vectorin main.o
 " typeinfo Element", :
     typeinfo Vectorin main.o
ld:
collect2: ld 1

, , , ( , )?

++ , vtables , .

+3
3

, / , Undefined symbols: "Element::plus(Element const&)". ( ) , .

+1

, "" "" "". "" "" "" , "= 0" . .

: , Element, , .

, , "" "" , . , . .

+3

This has nothing to do with Vector. You declare methods, such as virtual Element& plus(const Element&), and Element::Element()in Element.h, but you have to define them somewhere, presumably Element.cc. If you did this and you still get this error, it almost certainly means that you are not associating Element.o with your executable, so the linker simply does not know what to enter when Vector(or everything else) calls these methods.

+1
source

Source: https://habr.com/ru/post/1750912/


All Articles