如何修复const char与char DirectX11类型不兼容

时间:2018-02-05 07:30:54

标签: c++ directx-11

我知道这是一个愚蠢的问题,但我正在尝试为项目编译此代码,但我遇到了错误。我没有编写代码,我的项目是修改它以获得与代码最初产生的结果不同的结果。

但是,我甚至无法启动项目,因为给定的代码无法编译:严重级代码描述项目文件行抑制状态 错误(活动)E0167类型" const char *"与" char *"类型的参数不兼容Direct 3D Text c:\ Visual Studio Programs \ Direct 3D Text \ Direct 3D Text \ D3DTextDemo.cpp 30

我收到错误的代码:

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

由于

1 个答案:

答案 0 :(得分:1)

用双引号-eg。"TextureMap.fx"定义的char *称为字符串文字,并且是每个定义的常量。 将常量值传递给采用非常量值的函数时,无法保证它不会被修改,因此无法编译。

我通过在char []中定义文字修复了这个问题,这不再使它成为常量:

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