K.J. Shamberger
K.J. Shamberger

Reputation: 69

How to fix const char is incompatible with type char DirectX11

I know this is a dumb question, but I'm trying to compile this code for a project, but I run into an error. I didn't write the code, my project is to modify it to get a different outcome than what the code was originaly suppose to produce.

However, I can't even start the project because the given code won't compile: Severity Code Description Project File Line Suppression State Error (active) E0167 argument of type "const char *" is incompatible with parameter of type "char *" Direct 3D Text c:\Visual Studio Programs\Direct 3D Text\Direct 3D Text\D3DTextDemo.cpp 30

Code where I get the error:

bool D3DTextDemo::LoadContent()
{
    ID3DBlob* vsBuffer = 0;

    bool compileResult = CompileD3DShader("TextureMap.fx", "VS_Main", "vs_4_0", &vsBuffer);

    if (compileResult == false)
    {
        DXTRACE_MSG("Error compiling the vertex shader!");
        return false;
    }

    HRESULT d3dResult;

    d3dResult = d3dDevice_->CreateVertexShader(vsBuffer->GetBufferPointer(),
    vsBuffer->GetBufferSize(), 0, &solidColorVS_);

    if (FAILED(d3dResult))
    {
        DXTRACE_MSG("Error creating the vertex shader!");

        if (vsBuffer)
            vsBuffer->Release();

        return false;
    }

    D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
    };

    unsigned int totalLayoutElements = ARRAYSIZE(solidColorLayout);

    d3dResult = d3dDevice_->CreateInputLayout(solidColorLayout, totalLayoutElements,
        vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), &inputLayout_);

    vsBuffer->Release();

    if (FAILED(d3dResult))
    {
        DXTRACE_MSG("Error creating the input layout!");
        return false;
    }

    ID3DBlob* psBuffer = 0;

    compileResult = CompileD3DShader("TextureMap.fx", "PS_Main", "ps_4_0", &psBuffer);

    if (compileResult == false)
    {
        DXTRACE_MSG("Error compiling pixel shader!");
        return false;
    }

    d3dResult = d3dDevice_->CreatePixelShader(psBuffer->GetBufferPointer(),
    psBuffer->GetBufferSize(), 0, &solidColorPS_);

    psBuffer->Release();

    if (FAILED(d3dResult))
    {
        DXTRACE_MSG("Error creating pixel shader!");
        return false;
    }

    d3dResult = D3DX11CreateShaderResourceViewFromFile(d3dDevice_,
    "font.dds", 0, 0, &colorMap_, 0);

    if (FAILED(d3dResult))
    {
        DXTRACE_MSG("Failed to load the texture image!");
        return false;
    }

Thanks

Upvotes: 1

Views: 3738

Answers (1)

Rien B.
Rien B.

Reputation: 113

A char* defined in double quotes -eg."TextureMap.fx"- is called a string literal and is per definition constant. When you pass a constant value to a function that takes non-constant values, there is no guarantee that it won't be modified and thus won't compile.

I fixed this issue by defining the literal in a char[], which doesn't make it constant anymore:

char fxFile[] = "TextureMap.fx";
bool compileResult = CompileD3DShader(fxFile, "VS_Main", "vs_4_0", &vsBuffer);

Upvotes: 2

Related Questions