简体   繁体   中英

C++ Template Operator Overload Instantiation

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 st

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 st

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?

How do I go about instantiating all the required usage of operator^ st I can perform the following data types operation:

//Vector3D<double> = Vector3D<double> ^ Vector3D<double> or
//Vector3D<double> = Vector3D<Distance> ^ Vector3D<Force> or
//Vector3D<Moment> = Vector3D<Distance> ^ Vector3D<Force> (ideal!!!)

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>;

What you ask is doable: converting constructors in your Moment , Force , Distance classes, eg of the type Moment(const Force& A, const Distance& b) , would allow to feed Force s and Distance s to a Moment object.

Your code has too many const keywords in the operator^ declaration. Here below code that compiles; I have taken the liberty to make operator^ an external function, so that it accepts 2 arguments like a proper binary operator. I have also morphed your Vector3D class into a struct for practicality (encapsulation here is not really needed).

#include <iostream>

using namespace std;

//Vector3D.h
template <typename T>
struct Vector3D
{
    // data member
    T m_elem[3];

    // member functions
    Vector3D(const T& a, const T& b, const T& c): m_elem{a, b, c} {}

    template <typename Arg1, typename Arg2>
    friend Vector3D<double> operator^(const Vector3D<Arg1>& p_right, const Vector3D<Arg2>& p_left);
};

//Vector3D.cpp
// Calculate the cross product
template <typename Arg1, typename Arg2>
Vector3D<double> operator^(const Vector3D<Arg1>& p_right, const Vector3D<Arg2>& p_left)
{
    // Local variable
    Vector3D<double> cross_product(0,0,0);

    // Calculation
    cross_product.m_elem[0] = p_left.m_elem[1] * p_right.m_elem[2] - p_left.m_elem[2] * p_right.m_elem[1];
    cross_product.m_elem[1] = p_left.m_elem[2] * p_right.m_elem[0] - p_left.m_elem[0] * p_right.m_elem[2];
    cross_product.m_elem[2] = p_left.m_elem[0] * p_right.m_elem[1] - p_left.m_elem[1] * p_right.m_elem[0];

    return cross_product;
}


int main()
{
    Vector3D<double> a(8, 9, 0);
    Vector3D<double> b(18, 94, -50);
    Vector3D<double> cross = a^b;

    cout << "cross product is: [" << cross.m_elem[0] << ", "
         << cross.m_elem[1] << ", " << cross.m_elem[2] << "]\n";

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM