2013-10-15 133 views
0

我有以下結構vector.push_back()智能感知錯誤

struct foo{ 
    vector<foo*> cntns; 
}; 

和下面的函數

void createLink(foo *i1, foo *i2){ 
    i1->cntns.push_back(i2); 
    i2->cntns.push_back(i1); 
} 

,但我得到的錯誤

2 IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=foo*, _Alloc=std::allocator<foo*>]" matches the argument list 
     argument types are: (foo*) 
     object type is: std::vector<foo*, std::allocator<foo*>> 

代碼看起來編譯罰款,誰知道爲什麼會發生這種情況?

+2

旁註:喜歡'向量<性病::的unique_ptr >'如果可能的話。爲什麼在手動清理內容時使用矢量? –

+0

你能詳細說明你的意思嗎?不是'矢量>'矢量? – Lunyx

+1

如果你有一個指針的向量,並且這些指針(它們指向的內容)被動態分配,那麼在向量超出範圍之前,你必須遍歷該向量並「刪除」每個向量,否則「丟失」指針以其他方式。該向量不會爲您清理動態分配的內存,它會清理自己的存儲空間。它只是爲一堆指針分配存儲空間。 –

回答

1

不知道爲什麼這是一個Intellisense錯誤,因爲代碼會編譯並正常工作。

但是,如果你真的想擺脫智能感知錯誤的,我發現使它成爲一個成員函數擺脫投訴:

struct foo 
{ 
    vector<foo *> cntns; 

    void createLink(foo * i2) 
    { 
     this->cntns.push_back(i2); 
     i2->cntns.push_back(this); 
    } 
};