0

Looking to implement a cross-product operator^ oveload for template Vector3D class ideally that overload (though I think highly improbable/impossible) should be able to receive two different parameter types and return a third s.t.

    Vector3D<Distance> r;
    Vector3D<Force> F;
    Vector3D<Moment> M = r^F;

Since I assume that this is impossible and/or improbable I would be open to forcing the return parameter type to double s.t.

    Vector3D<Distance> r;
    Vector3D<Force> F;
    Vector3D<double> M = r^F;

However when I do that in the code below I get the following linking error even though template class Vector3D is instantiated:

1>Source.obj : error LNK2019: unresolved external symbol "public: class 
  Vector3D<double> const __thiscall Vector3D<double>::operator^<double>(class 
  Vector3D<double> const &)const " (??$?TN@?$Vector3D@N@@QBE?

Thank you in advance for the help.

    //Vector3D.h
    template <typename T>
    class Vector3D
    {
    public:
    '''
    template <typename Arg>
    const Vector3D<double> operator^ (const Vector3D<Arg>& p_other) const;
    '''
    private:
    //////////////////////////////////////////////////////////////////////////
    // *** PRIVATE DATA MEMBERS ***
    //////////////////////////////////////////////////////////////////////////
    T m_elem[3];

    }

    //Vector3D.cpp
    /// Calculate the cross product
    //
    template <typename T>
    template <typename Arg>
    const Vector3D<double> Vector3D<T>::operator^ (const Vector3D<Arg>& p_right) const
    {
        // Local variables
        Vector3D<T> left(*this);
        Vector3D<double> cross_product;
        // Calculation
        cross_product.m_elem[0] = left.m_elem[1] * p_right.m_elem[2] - left.m_elem[2] * p_right.m_elem[1];
        cross_product.m_elem[1] = left.m_elem[2] * p_right.m_elem[0] - left.m_elem[0] * p_right.m_elem[2];
        cross_product.m_elem[2] = left.m_elem[0] * p_right.m_elem[1] - left.m_elem[1] * p_right.m_elem[0];

        return cross_product;
    }

    template class Vector3D<double>;
hdamlaj
  • 51
  • 4
  • The problem in your case is that you try to support mixed parameter types. Your `operator^` is *still* a template, even if you explicitly instantiate the class. You have several options. (1) Do as the duplicate says. (2) Explicitly instantiate the specializations of `operator^` you want. (3) Avoid mixed arguments, and so it becomes a regular member that gets instantiated along with the enclosing class. – StoryTeller - Unslander Monica Sep 25 '20 at 21:21
  • Can you please provide an example of "(2)Explicitly instantiate the specializations of operator^ you want." for my case simply using the double type? Thank you so much. – hdamlaj Sep 26 '20 at 18:30

0 Answers0