2015-12-15 59 views
-5

我正在使用C++實現FP增長算法,但在使用向量時出現一些錯誤。這裏是我的代碼:向量推送錯誤

typedef struct Node 
{ 
    FPData *fpData; 
    int childCount; 
    struct Node *parent; 
    vector<Node*> childs; 
}FPNode; 

FPNode* FPTree::NewNode(string data, int support) 
{ 
    FPNode *node = (FPNode*) malloc(sizeof(FPNode)); 
    FPData *fpData = (FPData*) malloc(sizeof(FPData)); 
    fpData -> data = data; 
    fpData -> support = support; 
    node -> fpData = fpData; 
    node -> childCount = 0; 
    node -> parent = NULL; 
    return node; 
} 
void FPTree::InsertFPPath(FPNode* root, list<FPData*> fpDatas) 
{ 
    if(fpDatas.size() <= 0) 
    { 
     return; 
    } 
    bool flag = true; 
    for(int i = 0; i < root -> childCount; i++) 
    { 
     FPNode* child = root -> childs[i]; 
     if(child -> fpData -> data == fpDatas.front() -> data) 
     { 
      flag = false; 
      child -> fpData -> support ++; 
      fpDatas.pop_front(); 
      InsertFPPath(child, fpDatas); 
      break; 
     } 
    } 
    if(flag) 
    { 
     FPData* firstData = fpDatas.front(); 
     FPNode* newChild = NewNode(firstData -> data, firstData -> support); 
     root -> childs.push_back(newChild); // error at here 
     root -> childCount ++; 
     fpDatas.pop_front(); 
     for(int i = 0; i < fpDatas.size(); i++) 
     { 

      InsertFPPath(newChild, fpDatas); 
     } 
    } 
} 

InsertFPPath()是一個遞歸方法,我得到的錯誤,當第二次迭代:

root -> childs.push_back(newChild) 

的錯誤出現在:

construct(_Up* __p, _Args&&... __args) // here the __p is null, and __args is right 
{ 
     ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...); 
} 

和線程隊列停止at:

 
#0 0x00000001000050cf in void std::__1::allocator::construct(Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1673 

回溯是:

 
#0 0x00000001000050cf in void std::__1::allocator::construct(Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1673 
#1 0x00000001000050bc in void std::__1::allocator_traits >::__construct(std::__1::integral_constant, std::__1::allocator&, Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1600 
#2 0x000000010000509c in void std::__1::allocator_traits >::construct(std::__1::allocator&, Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1453 
#3 0x0000000100005079 in std::__1::vector >::push_back(Node* const&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1590 
#4 0x0000000100004fa9 in FPTree::InsertFPPath(Node*, std::__1::list >) at /Users/dorothy/ME/USTC/作業/數據挖掘/experiment/FPTree/FPTree/FPTree.cpp:129 
#5 0x00000001000051b4 in FPTree::InsertFPPath(Node*, std::__1::list >) at /Users/dorothy/ME/USTC/作業/數據挖掘/experiment/FPTree/FPTree/FPTree.cpp:135** 
+6

什麼錯誤?請具體說明。 – ajshort

+2

爲什麼在獲取孩子的size()時,你會跟蹤childCount?另外,當迭代一個'std :: vector'時,你應該使用迭代器,而不是索引訪問。 – tillaert

+0

@ user2435797您的C++看起來很像C.選擇一個並堅持下去。 – Biffen

回答

0

當分配用於結構(structclass)包含構造存儲器,或用於包含具有構造的對象的結構,則可以不使用分配的malloc存儲器。 malloc函數只分配內存,但它不調用構造函數,在您的情況下,這意味着childs成員對象將不會構造,並且使用該向量將導致未定義的行爲

動態時在C++中,你需要使用new運營商分配內存:

FPNode *node = new FPNode; 
FPData *fpData = new FPData; 
+0

錯誤仍然存​​在... – user2435797