2014-11-03 68 views
0

我在这里有一个问题。 此功能:函数与C++ 11规范不兼容

BOOL WINAPI SFileGetFileName(HANDLE hFile, char * szFileName) { 
    TMPQFile * hf = (TMPQFile *)hFile; // MPQ File handle 
    char *szExt = "xxx";    // Default extension 
    DWORD dwFirstBytes[2];    // The first 4 bytes of the file 
    DWORD dwFilePos;     // Saved file position 

    int nError = ERROR_SUCCESS; 
    int i; 

    // Pre-zero the output buffer 
    if(szFileName != NULL) 
     *szFileName = 0; 

    // Check valid parameters 
    if(nError == ERROR_SUCCESS) 
    { 
     if(hf == NULL || szFileName == NULL) 

      nError = ERROR_INVALID_PARAMETER; 

    } 



    // If the file name is already filled, return it. 

    if(nError == ERROR_SUCCESS && *hf->szFileName != 0) 

    { 

     if(szFileName != hf->szFileName) 

      strcpy(szFileName, hf->szFileName); 

     return TRUE; 

    } 



    if(nError == ERROR_SUCCESS) 

    { 

     if(hf->dwBlockIndex == (DWORD)-1) 

      nError = ERROR_CAN_NOT_COMPLETE; 

    } 



    // Read the first 8 bytes from the file 

    if(nError == ERROR_SUCCESS) 

    { 

     dwFirstBytes[0] = dwFirstBytes[1] = 0; 

     dwFilePos = SFileSetFilePointer(hf, 0, NULL, FILE_CURRENT); 

     SFileReadFile(hFile, &dwFirstBytes, sizeof(dwFirstBytes), NULL); 

     BSWAP_ARRAY32_UNSIGNED(dwFirstBytes, sizeof(dwFirstBytes)/sizeof(DWORD)); 

     SFileSetFilePointer(hf, dwFilePos, NULL, FILE_BEGIN); 

    } 



    if(nError == ERROR_SUCCESS) 

    { 

     if((dwFirstBytes[0] & 0x0000FFFF) == ID_EXE) 

      szExt = "exe"; 

     else if(dwFirstBytes[0] == 0x00000006 && dwFirstBytes[1] == 0x00000001) 

      szExt = "dc6"; 

     else 

     { 

      for(i = 0; id2ext[i].szExt != NULL; i++) 

      { 

       if(id2ext[i].dwID == dwFirstBytes[0]) 

       { 

        szExt = id2ext[i].szExt; 

        break; 

       } 

      } 

     } 



     // Create the file name 

     sprintf(hf->szFileName, "File%08lu.%s", hf->dwBlockIndex, szExt); 

     if(szFileName != hf->szFileName) 

      strcpy(szFileName, hf->szFileName); 

    } 

    return (nError == ERROR_SUCCESS); 

} 

给我上 '制作' 这些错误:

SFileReadFile.cpp: In function ‘bool SFileGetFileName(HANDLE, char*)’: 
SFileReadFile.cpp:655:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 
char *szExt = "xxx";    // Default extension 
      ^
SFileReadFile.cpp:700:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 
     szExt = "exe"; 
      ^
SFileReadFile.cpp:702:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 
     szExt = "dc6"; 
      ^
SFileReadFile.cpp:716:72: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘DWORD {aka unsigned int}’ [-Wformat=] 
    sprintf(hf->szFileName, "File%08lu.%s", hf->dwBlockIndex, szExt); 

请给我提示,如何解决这些?

我已经在C++文档中尝试了很多,但我没有太多的运气来找到需要的东西。看,我不能在szext的声明中做const char const*,因为我得到很多关于常量的错误。但我真的想摆脱这些错误。

请给我一些建议或为什么发生更深的解释。

+4

'char const * szExt =“xxx”;'工作吗? (注意额外的'const')。一个字符串数组“'xx”'是一个'char const []',并且转换到'char *'的过程大概是C++ 14。使用'char const *'应该是首选。 – Niall 2014-11-03 09:24:38

+0

你说得对,'szExt'不应该被定义为'const char * const szExt',但是你有什么建议让你觉得这样做?了解你的理解将允许更有用的答案。 – hvd 2014-11-03 09:24:48

+0

@Niall是的,没错,但不幸的是,像这样的大多数问题,只是给出正确的答案并没有帮助任何人真正理解正在发生的事情。 (也许这里有所不同。) – hvd 2014-11-03 09:26:28

回答

0

这些不是您收到警告消息的错误,并且您的程序将正常工作。

在C++ 11中,您正在使用的初始化字符串被删除。

见其他答案,或者您可以使用std::string str="xxx";

,但你需要包含头用C

+2

我想摆脱这些警告,因为他们太烦人了;) – Duosora 2014-11-03 09:30:51

0

字符串字面++有常量字符数组类型。所以你必须使用一个指向const char的指针,如果你打算把一个字符串文字分配给一个字符指针。

所以它会更正确地写

const char *szExt = "xxx"; 

或者你可以使用一个字符数组,而不是指针,如果你不想使用标识const

char szExt[] = "xxx"; 

要考虑到任何修改字符串文字的尝试都会导致未定义的行为。

+0

const char *抛出另一个警告,但char const *不会。看起来很奇怪。 – Duosora 2014-11-03 09:37:07

+0

@Duosora const char *和char const *是等价的声明序列。编译器不能针对这些记录发出警告。 – 2014-11-03 09:39:16