2015-07-20 52 views
-4

由於void *無法分配給類型爲char的實體* 16 應該如何處理解決錯誤。問題出現在xmlpath和dllPath中智能感知:無法將類型爲「void *」的值分配給類型爲「char *」的實體16

void fmuLoad() { 
    char* fmuPath; 
char tmpPath[1000]="W:\\Prajwal\\GM_FMU_EXTRACT\\"; 
char* xmlPath; 
char* dllPath; 
const char *modelId; 
FMU fmu; 

fmuUnzip(); 
// parse tmpPath\modelDescription.xml 
xmlPath = calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1); 
sprintf(xmlPath, "%s%s", tmpPath, XML_FILE); 
fmu.modelDescription = parse(xmlPath); 
free(xmlPath); 
if (!fmu.modelDescription) exit(EXIT_FAILURE); 
//printf(fmu.modelDescription); 
#ifdef FMI_COSIMULATION 
modelId = getAttributeValue((Element*)getCoSimulation(fmu.modelDescription),att_modelIdentifier); 

//#else // FMI_MODEL_EXCHANGE 
// modelId = getAttributeValue((Element *)getModelExchange(fmu.modelDescription), att_modelIdentifier); 
#endif 
// load the FMU dll 
dllPath = calloc(sizeof(char), strlen(tmpPath) + strlen(DLL_DIR) + strlen(modelId) + strlen(".dll") + 1); 
sprintf(dllPath, "%s%s%s.dll", tmpPath, DLL_DIR, modelId); 
if (!loadDll(dllPath, &fmu)) { 
    exit(EXIT_FAILURE); 
} 
// free(dllPath); 
// free(fmuPath); 
// free(tmpPath); 

} 
+0

不是功能,抱歉,但你可以看到dllPath = calloc **那裏顯示錯誤 – PrajwalBhat

回答

0

在C++中,需要強制轉換來分配一個空指針。

xmlPath = (char*)calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1); 

或者,使用C++風格:

xmlPath = static_cast<char*>(calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1)); 

當然,一個真的應該質疑爲什麼你正在使用舊的C庫函數像calloc可言。如果你實際編譯一個C程序,試着告訴你的編譯器它是C而不是C++。那麼鑄件就沒有必要了。

0
static_cast<char*>(calloc(sizeof(char), strlen(tmpPath) + strlen(DLL_DIR) + strlen(modelId) + strlen(".dll") + 1)); 

calloc的返回類型是void *。你必須顯式地施加calloc的結果。

相關問題