【问题标题】:Error C2664: 'auxDIBImageLoadW' : cannot convert parameter 1 from 'LPSTR' to 'LPCWSTR'错误 C2664:“auxDIBImageLoadW”:无法将参数 1 从“LPSTR”转换为“LPCWSTR”
【发布时间】:2015-01-06 19:56:21
【问题描述】:

我只是在重新安装 windows 和 VS 2012 Ultimate 后修改代码。代码(如下所示)之前工作得很好,但是当我现在尝试运行代码时,它给出了以下错误:

Error   1   error C2664: 'auxDIBImageLoadW' : cannot convert parameter 1 from 'LPSTR' to 'LPCWSTR'  

代码:

void CreateTexture(GLuint textureArray[], LPSTR strFileName, int textureID)
{
    AUX_RGBImageRec *pBitmap = NULL;
    if (!strFileName)                                   // Return from the function if no file name was passed in
        return;
    pBitmap = auxDIBImageLoad(strFileName);     //<-Error in this line      // Load the bitmap and store the data

    if (pBitmap == NULL)                                    // If we can't load the file, quit!
        exit(0);

    // Generate a texture with the associative texture ID stored in the array
    glGenTextures(1, &textureArray[textureID]);

    // This sets the alignment requirements for the start of each pixel row in memory.
    //  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

    // Bind the texture to the texture arrays index and init the texture
    glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);

    // Build Mipmaps (builds different versions of the picture for distances - looks better)
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data);

    // Lastly, we need to tell OpenGL the quality of our texture map.  GL_LINEAR is the smoothest.
    // GL_NEAREST is faster than GL_LINEAR, but looks blochy and pixelated.  Good for slower computers though.
    // Read more about the MIN and MAG filters at the bottom of main.cpp

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    //  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);


    // Now we need to free the bitmap data that we loaded since openGL stored it as a texture

    if (pBitmap)                                        // If we loaded the bitmap
    {
        if (pBitmap->data)                              // If there is texture data
        {
            free(pBitmap->data);                        // Free the texture data, we don't need it anymore
        }

        free(pBitmap);                                  // Free the bitmap structure
    }
}

我试过这个Link,This one too,也试过this一个。但仍然出现错误。

这个函数在初始化后使用如下:

LPCWSTR k =L"grass.bmp";
CreateTexture(g_Texture, "building1.bmp", 0);
CreateTexture(g_Texture, "clock.bmp", 0);
//list goes on

你能帮帮我吗?

【问题讨论】:

  • 为什么要将窄字符串传递给宽函数?

标签: c++ opengl visual-studio-2012


【解决方案1】:

将“LPSTR strFileName”更改为“LPCWSTR strFileName”,将“building1.bmp”更改为 L“building1.bmp,将“clock.bmp”更改为 L“clock.bmp”。

请务必小心,因为 LPSTR 是 ASCII 而 LPCWSTR 是 Unicode。所以如果函数需要一个 Unicode 变量(比如:L"String here"),你不能给它一个 ASCII 字符串。

【讨论】:

    【解决方案2】:

    解决方案是:

    更改您的函数原型以采用宽字符串:

    void CreateTexture(GLuint textureArray[], LPWSTR strFileName, int textureID)
    //...
    LPCWSTR k =L"grass.bmp";
    CreateTexture(g_Texture, L"building1.bmp", 0);
    CreateTexture(g_Texture, L"clock.bmp", 0);
    

    或

    不要改变你的函数原型,而是调用A版本的API函数:

    pBitmap = auxDIBImageLoadA(strFileName);  
    

    推荐:坚持使用宽字符串并使用正确的字符串类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多