2012-11-04 28 views
0

這裏是我的結構(這是數據結構類的私有部分):如何用空值初始化結構的數據成員數組?

private: 
// this is the main node pointer array to keep 
// track of buckets and its chain. 
    struct Node { 

    /** member variables */ 
    unsigned int iCID; 
    std::string sVoting_PN; 
    std::string sTS[T_LITTRAL]; 
    unsigned int iCounter[12] = {0}; 


    /** constructor initializes */ 
    Node(unsigned int CID, std::string PN, std::string TS, unsigned int counter, unsigned int index) { 
    this->iCID = CID; 
    this->sVoting_PN = PN; 
    this->sTS[index] = TS; 
    this->iCounter[CID] = iCounter[CID] + counter; 
} 

Node *next; 
}; 
Node* nodeArray[TABLE_SIZE]; 

我想我該怎麼做,要iCounter陣列的所有值初始化爲0?

我想這一個

unsigned int iCounter[12] = {0}; 

,但它給我警告,我不得不刪除,無論如何!任何想法或幫助是非常感激的人。提前致謝。

+3

明確你得到什麼警告?我得到無[在這縮短了測試(http://liveworkspace.org/code/e6e5a598130d40a3ac6a8c0322a3e219)(假定這就是你真正把'= {0}')。 – Xeo

+0

@Xeo,這裏的warnigs是 ######################################### ##### 警告:只有-std = C++ 11或-std = gnu ++ 11可用的非靜態數據成員初始值設定項[默認情況下啓用] 警告:擴展初始值設定項列表僅適用於-std = C++ 11或-std = gnu ++ 11 [默認啓用] – johnshumon

+0

那麼,爲什麼不編譯代碼時只包含...'-std = C++ 11'? – Xeo

回答

0

它的工作倒過來。以下行是問題。

unsigned int iCounter[12] = {0}; 

經過很多不同的嘗試,最後我得到了這種初始化工作正常沒有任何警告和錯誤的方式。我將此添加到答案部分,以便其他有類似問題的人可以從中獲得幫助。

struct Node { 
Node(): iCID(0), sVoting_PN("") { 

    for(unsigned int l=0;l<T_LITTRAL;l++){ 

    sTS[l]=""; 
    if(l<AMOUNT_OF_ARTISTS){ 
     iCounter[l]=0; 
    } 
    } 

} 

    //member variables 
    unsigned int iCID; 
    std::string sVoting_PN; 
    std::string sTS[T_LITTRAL]; 
    unsigned int iCounter[AMOUNT_OF_ARTISTS]; 

    Node *next; 
};