Calling a function from a derived template class

梦想与她 提交于 2019-12-04 15:26:38

I think the compiler/linker means it when it tells you Undefined symbols: "Element::plus(Element const&)" . These symbols (plus and minus for Element) have been declared but they have not been defined.

If I understand well, you want to have an abstract "Element", and derive "Vector" from "Element". In that case, you should remove your "Element" constructor, and declare "plus" and "minus" to be pure virtual by adding "= 0" at the end of the declaration. Your destructor is fine, though.

That's all the error message is about: as you declared a constructor and some methods in Element, and you end up calling them, the linker looks for them.

I've got the feeling that you would want your Vector class to implement "plus" and "minus" on a Vector, and not on an abstract Element, though. This way, you would not need the static cast. You'd also avoid a world of pain with massive risks of slicing with your return types.

This really has nothing to do with Vector. You declare methods like virtual Element& plus(const Element&) and Element::Element() in Element.h, but you have to define them somewhere, presumably in Element.cc. If you've done that and you're still getting this error, it almost certainly means that you're not linking Element.o into your executable, so the linker simply doesn't know what to put in when Vector (or anything else) invokes these methods.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!