[英]Insert a dynamically created struct item into a vector of structs
我正在嘗試用動態創建的struct元素填充結構的向量。 下面的所有代碼都包含在名為TD_Dijkstra_OS的類中
結構:
struct instFunDescLeg
{
float Ap, Bp, tfail;
typename GraphType::NodeIterator n; //external library data type
};
我計算將項目的fiels分配到函數中所需的值:
v = G.target( e);
FunDataType Ap, Bp, offset, slope;
v->dist = getEarliestArrivalTime( e, u->dist, slope, offset); //TEST
//if( getEarliestArrivalTime( e, u->dist, slope, offset) != v->dist)
// continue;
Ap = ( 1 + slope) * u->Ap;
Bp = ( 1 + slope) * u->Bp + offset;
if( v->timestamp != (*m_timestamp))
{
v->Ap = Ap;
v->Bp = Bp;
Q.push( v);
v->timestamp = (*m_timestamp);
}
else
{
if( Ap < v->Ap)
{
v->Ap = Ap;
v->Bp = Bp;
}
else if( Ap == v->Ap && Bp > v->Bp)
v->Bp = Bp;
}
v->tfail = v->dist;
storeInstFunDescLeg(v);
然后我創建一個struct項並嘗試將其插入到instFunDescLeg項的向量中,聲明為:
std::vector<struct instFunDescLeg> bp;
進入這個功能:
void storeInstFunDescLeg(const NodeIterator& u)
{
//representation: (idn:(Ap(tfail),Bp(tfail),idfn(tfail))), for a node identified by idn
//store into a vector
instFunDescLeg* leg;
leg = new instFunDescLeg();
leg->Ap = u->Ap;
leg->Bp = u->Bp;
leg->tfail = u->tfail;
leg->n = u;
bp.push_back(leg);
}
當我編譯它時,我收到一條錯誤消息,指出沒有匹配bp.push_back(leg)的函數。 錯誤消息后的注釋說明了這種情況:
/usr/include/c++/4.6/bits/stl_vector.h:826:7: σημείωση: no known conversion for
argument 1 from ‘TD_Dijkstra_OS<DynamicGraph<AdjacencyListImpl, node, edge>
:: instFunDescLeg *'to'const value_type&{aka const TD_Dijkstra_OS> :: instFunDescLeg&}'
有人可以幫我實現插入程序嗎?
std::vector<struct instFunDescLeg> bp;
應寫成std::vector<instFunDescLeg> bp;
你的結構非常簡單,所以我認為沒有必要使用你提供的內容存儲一個指針向量。 如果有一些隱藏的要求會強制您使用指針,請嘗試將它們隱藏在智能指針類后面(例如unique_ptr
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.