dimanche 2 juillet 2023

I cannot call operator- function defined outside my class from my class

I came across an issue with overloading operator.. methods. I can call them with no problem but I couldn't call it inside the "reject" method which i don't see why.
I would be very pleased if you explain why. I really appreciate any help you can provide.

ps: when I comment out the reject method I could call the method to print the difference of the vectors but I cannot see why I cannot call them from a static method. I couldn't find any answers and have been thinking for days.

// vector3d.h

namespace gm::data // graphics math
{
    struct Vector3D
    {
       ...  
        // Projection of first onto second vector.
        static inline Vector3D project(const Vector3D &first, const Vector3D &second)
        {
            return (second * (dot(first, second) / dot(second, second)));
        }

        // Reject of first onto second vector.
        static inline Vector3D reject(const Vector3D &first, const Vector3D &second)
        {
            return first - (second * (dot(first, second) / dot(second, second)));
        }
...

    private:
        double xyz[3];
    };

 
    static inline Vector3D operator-(const Vector3D &xyz, const Vector3D &other)
    {
        return Vector3D(xyz[0] - other[0], xyz[1] - other[1], xyz[2] - other[2]);
    }

}


//Test.cc file is below 

const Vector3D vector{1, 2, 3};
const Vector3D vector2{1, 5, 7};

std::cout << vector - vector2 << std::endl;
std::cout << gm::data::operator-(vector, vector2) << std::endl;


In file included from ./src/main.cc:3:
./src/data/vector3d: In member function ‘gm::data::Vector3D gm::data::Vector3D::reject(const gm::data::Vector3D&, const gm::data::Vector3D&)’:
./src/data/vector3d:86:26: error: no match for ‘operator-’ (operand types are ‘const gm::data::Vector3D’ and ‘gm::data::Vector3D’)
   86 |             return first - (second * (dot(first, second) / dot(second, second)));
      |                    ~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                    |               |
      |                    |               gm::data::Vector3D
      |                    const gm::data::Vector3D
In file included from ./test/test.cc:2:
./src/data/vector3d: In member function ‘gm::data::Vector3D gm::data::Vector3D::reject(const gm::data::Vector3D&, const gm::data::Vector3D&)’:
./src/data/vector3d:86:26: error: no match for ‘operator-’ (operand types are ‘const gm::data::Vector3D’ and ‘gm::data::Vector3D’)
   86 |             return first - (second * (dot(first, second) / dot(second, second)));
      |                    ~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                    |               |
      |                    |               gm::data::Vector3D
      |                    const gm::data::Vector3D
make: *** [Makefile:2: all] Error 1

Aucun commentaire:

Enregistrer un commentaire