2016-04-24 63 views
-5

我想要爲我的noeud分配內存(成功)動態內存分配的C++字符串

NOEUD* CreerNoeud(DF x) 
{ 
    NOEUD* pn = (NOEUD*)malloc(sizeof(NOEUD)); 

    if (pn == NULL) 
    { 
     printf("probleme allocation, on arrete \n"); 
    } 
    else 
    { 
     // Allocation valide 

     pn->data= x; 
     pn->data = x; 


     pn->suivant = NULL; 
    } 

    return pn; 
} 

,但我有我的結構串
「錯誤讀取字符的數據存儲問題。 「

typedef struct DF 
{ 
    std::string Lside; 
    std::string Rside; 

}DF; 
typedef struct NOEUD 
{ 
    DF data; 
    struct NOEUD* suivant; 
}NOEUD; 
+1

'pn-> data = x;'next line'#pn-> data = x;'你有快門嗎? –

+0

當你使用'std :: string'時,爲什麼你把它標記爲'c'? –

+1

你想要C或C++嗎?選擇適當的語言。 –

回答

0

當你分配它時,你的struct DF的字符串被初始化爲空。 你在嘗試閱讀這些字符串之前是否設置了這些字符串?

+0

我從用戶
獲得輸入,然後我將數據添加到新的noeud – shikova

+0

您可以顯示代碼將輸入存儲到字符串中的代碼以及您正在閱讀的代碼中嗎?如果沒有,那麼幫助你並不容易。 – SeB

+0

function.cpp http://textuploader.com/5yzgf | function.h文件ht​​tp://textuploader.com/5yzgc | main.cpp文件http://textuploader.com/5yzgg | – shikova