共用方式為


C6284

更新:2007 年 11 月

警告 C6284: 傳遞物件做為參數 '%d',但 <function> 呼叫中需要字串。

這個警告表示格式字串 (Format String) 指定字串 (例如,printf 或 scanf 的 %s 規格),但傳遞的卻是 C++ 物件。

這項缺失可能會產生不正確的輸出或損毀。

會報告這個訊息通常是因為將實作某個字串型別的 C++ 物件 (例如,std::string、CComBSTR 或 bstr_t) 傳遞給 C printf-Style 呼叫。視 C++ 類別的實作 (Implementation) 而定,如果已定義正確的轉型 (Cast) 運算子,則只要需要 C 字串,通常就可無障礙地使用 C++ 字串物件。不過,因為 printf-Style 函式的參數必須是不具型別的,所以不會轉換為字串。

視物件而定,可能會適合將 static_cast 運算子插入至適當的字串型別 (例如, char * 或 TCHAR*),或呼叫傳回字串 (例如,std::string 之執行個體 (Instance) 上的 c_str()) 的成員 (Member) 函式。

範例

下列程式碼會產生這個警告,原因是將 CComBSTR 傳遞給 sprintf 函式:

#include <atlbase.h>
#include <stdlib.h>

void f()
{
  char buff[50];
  CComBSTR bstrValue("Bye");
 
  sprintf(buff,"%ws",bstrValue); 
}

下列程式碼會使用靜態轉型來更正這個警告:

#include <atlbase.h>
#include <stdlib.h>

void f()
{
  char buff[50];
  CComBSTR bstrValue("Bye");
  
  sprintf_s(buff,50,"%ws",static_cast<wchar_t *>(bstrValue));
}

請參閱

參考

static_cast Operator

sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l