2014-11-03 115 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