DirectXMath.h operator ambiguity

1.5k Views Asked by At

I recently wrote a matrix class to deal with XMFloat4x4 and XMVector transformations. It was working great on windows 8 metro apps but when I tried to port it to Win32 Windows application, it started giving error

"error C2593: 'operator *' is ambiguous c:\program files (x86)\windows kits\8.1\include\um\DirectXMathMisc.inl", and there were a lot many of these errors related to more than one instance of overloaded function XMFunctions

The problem occurs when including "DirectXMath.h"

please let me know whats wrong

MATRIX4X4.h

#pragma once

#include <btBulletCollisionCommon.h>
#include <cmath>
#include <DirectXMath.h>

using namespace DirectX;
#define _USE_MATH_DEFINES

class MATRIX4X4
{
private:
    XMFLOAT4X4 _m;
public:
    MATRIX4X4();
    MATRIX4X4(const XMFLOAT4X4 &data);
    MATRIX4X4(float* data);

    void InitializeIdentity();
    MATRIX4X4& InitTranslation(float x, float y, float z);
    MATRIX4X4& InitTranslation(const XMFLOAT3 &pos);
    MATRIX4X4& InitScaling(float x, float y, float z);
    MATRIX4X4& InitScaling(const XMFLOAT3 &scl);
    MATRIX4X4& InitRotation(float x, float y, float z);
    MATRIX4X4& InitRotation(const XMVECTOR &quaternion);
    MATRIX4X4& InitPerspective(float minimumFieldOfView, float aspectRatio, float nearPlane, float farPlane);
    MATRIX4X4& InitOrthographic(float left, float right, float bottom, float top, float nearPlane, float farPlane);
    MATRIX4X4& InitView(const XMFLOAT3 &eyePosition, const XMFLOAT3 &lookPosition, const XMFLOAT3 &up);

    MATRIX4X4& SetTranslation(float x, float y, float z);
    MATRIX4X4& SetTranslation(const XMFLOAT3 &pos);
    MATRIX4X4& Translate(float dx, float dy, float dz);
    MATRIX4X4& Translate(const XMFLOAT3 &dp);

    MATRIX4X4& SetScale(float x, float y, float z);
    MATRIX4X4& SetScale(const XMFLOAT3 &scl);
    MATRIX4X4& Scale(float dx, float dy, float dz);
    MATRIX4X4& Scale(const XMFLOAT3 &ds);

    MATRIX4X4& SetRotation(float x, float y, float z);
    MATRIX4X4& SetRotation(const XMFLOAT3 &rot);
    MATRIX4X4& SetRotation(const XMVECTOR &quaternion);
    MATRIX4X4& Rotate(float dx, float dy, float dz);
    MATRIX4X4& Rotate(const XMFLOAT3 &dr);
    MATRIX4X4& Rotate(const XMVECTOR &quaternion);

    MATRIX4X4& operator* (MATRIX4X4 mat);
    MATRIX4X4& operator*= (MATRIX4X4 mat);

    void LookAt(const XMFLOAT3& position, const XMFLOAT3& target, const XMFLOAT3& up);

    // for bulllet physics //
    MATRIX4X4& Translate(const btVector3 &dp);
    MATRIX4X4& SetTranslation(const btVector3 &pos);
    MATRIX4X4& Scale(const btVector3 &ds);
    MATRIX4X4& SetScale(const btVector3 &scl);
    MATRIX4X4& Rotate(const btQuaternion &quaternion);
    MATRIX4X4& SetRotation(const btQuaternion &quaternion);
    /////////////////////////

    const XMFLOAT4X4& GetData() const;

    const XMFLOAT3& GetTranslation() const;
    const XMFLOAT3& GetScale() const;
    const XMVECTOR& GetRotation() const;
};
3

There are 3 best solutions below

4
On

Do you have a using namespace DirectX; in your .cpp files? Be sure you do not have using namespace DirectX; in a header file or before you do #include <DirectXMath.h>.

You might also want to take a look at SimpleMath in the DirectX Tool Kit.

EDIT: Be sure you are not trying to include both XNAMath.h and DirectXMath.h in the same application. You should use DirectXMath.h only.

1
On

so I just needed to define _XM_NO_INTRINSICS_ before include DirectXMath.h

#define _XM_NO_INTRINSICS_ 1;

for more information: https://msdn.microsoft.com/en-us/library/windows/desktop/ee415579(v=vs.85).aspx

0
On

Boom:

#define BT_NO_SIMD_OPERATOR_OVERLOADS

(from https://social.msdn.microsoft.com/Forums/sqlserver/en-US/5d85c350-f5cc-46c5-9924-0b07d1cd674e/directxmathh-operator-ambiguity?forum=windowssdk, I assume you got your answer ? :) I'll leave that here for reference,)

The xm-no-intrinsics hack did the trick for me.

I think the problem is actually the #include <btBulletCollisionCommon.h>

I've just incorporated bullet in my own project and I started having the same errors. I do have #using namespace DirectX

You don't show the rest of the message, but for me the complete errors were:

1>c:\users\germain\documents\work\pp\editor\camera\camera.h(27): error C2593: 'operator +' is ambiguous 1> c:\users\germain\documents\work\pp\physics\src\bulletdynamics\constraintsolver\btsolverbody.h(97): note: could be 'btSimdScalar operator +(const btSimdScalar &,const btSimdScalar &)' 1> c:\users\germain\documents\work\pp\physics\src\linearmath\btmatrix3x3.h(858): note: or 'btMatrix3x3 operator +(const btMatrix3x3 &,const btMatrix3x3 &)' 1> c:\users\germain\documents\work\pp\physics\src\linearmath\btvector3.h(765): note: or 'btVector3 operator +(const btVector3 &,const btVector3 &)' 1> c:\users\germain\documents\work\pp\physics\src\linearmath\btscalar.h(323): note: or '__m128 operator +(const __m128,const __m128)' 1> c:\program files (x86)\windows kits\8.1\include\um\directxmathvector.inl(13945): note: or 'DirectX::XMVECTOR DirectX::operator +(DirectX::FXMVECTOR,DirectX::FXMVECTOR)' 1> c:\users\germain\documents\work\pp\editor\camera\camera.h(27): note: while trying to match the argument list '(const DirectX::XMVECTOR, const DirectX::XMVECTOR)'

so it seems to make sense. #define BT_NO_SIMD_OPERATOR_OVERLOADS, in bullet, is the way to not pollute your namespace from there,