2013-10-15 92 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); 
    } 
};