2016-03-01 67 views
0

我正在嘗試這段代碼。我所試圖做的是在編譯時我收到以下錯誤,從類型結構從類型結構的向量中刪除元素

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <boost/bind.hpp> 

using namespace std; 

typedef struct _detail 
{ 
    int y; 
}detail; 

typedef struct _list 
{ 
    int x; 
    detail det; 
}list; 

std::vector<list> v; 
list li[5]; 

void PushElements() 
{ 
    for(int i = 0; i < 5; i++) 
    { 
     li[i].x = i+2; 
     li[i].det.y = i+3; 
     v.push_back(li[i]); 
    } 
} 

void display() 
{ 
    for(int i = 0; i < v.size(); i++) 
    { 
     cout << v[i].x << " "; 
     cout << v[i].det.y << " "; 
    } 
    cout << endl; 
} 


void DeleteElement() 
{ 


std::vector<list>::iterator it = std::find_if(v.begin(), v.end(), boost::bind(&list::detail::y, _1) == 3); 
v.erase(it); 

} 

int main() 
{ 
    PushElements(); 
    display(); 
    DeleteElement(); 
    cout << "After Deleting...................." << endl; 
    display(); 

    return 0; 
} 

的矢量刪除元素:

error C3083: 'detail': the symbol to the left of a '::' must be a type 
error C2039: 'y' : is not a member of '_list' 
error C2065: 'y' : undeclared identifier 

我不明白這是什麼錯誤,以及如何解決它。有人可以幫我解決這個錯誤嗎?

回答

2
&list::detail::y 

detail是未嵌套在list一個類型的名稱。 list擁有類型的成員detail名爲y

我想你想形成一個指向成員的指針,但據我所知,這是不可能的。你最好使用lambda進行比較。例如:

std::find_if(..., ..., [something](const list& l) { return l.det.y == something; }); 

在另一個說明中,以下劃線開頭的標識符保留在全局名稱空間中。 _list_detail不是嚴格允許的。