1
\$\begingroup\$

I'm following the "3D game programming with DirectX 11" book (Frank Luna) and i'm having this issue. There is no valid constructor to convert from (const float*) to DirectX::XMFLOAT4

namespace Colors {

    XMGLOBALCONST XMVECTORF32 White = { 1.0f, 1.0f, 1.0f, 1.0f };
    XMGLOBALCONST XMVECTORF32 Black = { 0.0f, 0.0f, 0.0f, 1.0f };
    XMGLOBALCONST XMVECTORF32 Red = { 1.0f, 0.0f, 0.0f, 1.0f };
    XMGLOBALCONST XMVECTORF32 Green = { 0.0f, 1.0f, 0.0f, 1.0f };
    XMGLOBALCONST XMVECTORF32 Blue = { 0.0f, 0.0f, 1.0f, 1.0f };
    XMGLOBALCONST XMVECTORF32 Yellow = { 1.0f, 1.0f, 0.0f, 1.0f };
    XMGLOBALCONST XMVECTORF32 Cyan = { 0.0f, 1.0f, 1.0f, 1.0f };
    XMGLOBALCONST XMVECTORF32 Magenta = { 1.0f, 0.0f, 1.0f, 1.0f };

    XMGLOBALCONST XMVECTORF32 Silver = { 0.75f, 0.75f, 0.75f, 1.0f };
    XMGLOBALCONST XMVECTORF32 LightSteelBlue = { 0.69f, 0.77f, 0.87f, 1.0f };
}

The error is here

Vertex vertices[] = {
    { XMFLOAT3(-1.0f, -1.0f, -1.0f), (const float*)&Colors::White },
    {XMFLOAT3(-1.0f, +1.0f, -1.0f), (const float*)&Colors::Black },
    {XMFLOAT3(+1.0f, +1.0f, -1.0f), (const float*)&Colors::Red },
    {XMFLOAT3(+1.0f, -1.0f, -1.0f), (const float*)&Colors::Green},
    {XMFLOAT3(-1.0f, -1.0f, +1.0f), (const float*)&Colors::Blue},
    {XMFLOAT3 (-1.0f, +1.0f, +1.0f), (const float*)&Colors::Yellow},
    {XMFLOAT3 (+1.0f, +1.0f, +1.0f), (const float*)&Colors::Cyan},
    {XMFLOAT3 (+1.0f, -1.0f, +1.0f), (const float*)&Colors::Magenta}
};

and this is the struct Vertex

struct Vertex {
        XMFLOAT3 Pos; XMFLOAT4 Color;
    };
\$\endgroup\$
1
\$\begingroup\$

XMFLOAT4 only has a constructor taking a const float* when the compiler is compiling the file as C++. If the constructor isn't visible, the TU being compiled when the error is generated may not be compiled as C++.

It's also possible you have a version of the header where the constructor is marked explicit, which means you'll need to explicitly specify that you are invoking it: XMVECTOR4((const float*)&Colors::Red) for example.

Without further information it's really impossible to say more.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Josh Petrie♦ Man you are a genius or something, you solved both my errors (also in another post) Basically i had to XMFLOAT4((const float*)&Colors::Red) the thing, (not XMVECTOR4((const float*)&Colors::Red) ) you probably just got confused one second but still you helped me, thanks a lot. I don't understand why is like this, but it works \$\endgroup\$ – Emanuele Jan 22 '17 at 16:55
  • \$\begingroup\$ btw, what is the purpose of the & in (const float*)&Colors::Red? The code compiles and works with or without it. Is it just for semantic purposes, showing that the conversion is from a pointer to XMVECTORF32 into a pointer to float? \$\endgroup\$ – Daniel Nitzan Jul 10 '17 at 2:11
  • \$\begingroup\$ Answering myself: this is a reinterpretation cast style, i.e. changing from one pointer type to another point type. In this particular case there is no need to get the address of Colors::Red before casting to pointer to float, because this is an overloaded cast operator from the actual value of Colors::Red. However, translating this to the C++ syntax, ampersand must be present as in: reinterpret_cast<const float*>(&Colors::Red), otherwise it doesn't compile. Also see stackoverflow.com/questions/1952471/ampersand-inside-casting \$\endgroup\$ – Daniel Nitzan Jul 17 '17 at 0:30

Your Answer

By clicking ā€œPost Your Answerā€, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.