我正在尝试用动态创建的struct元素填充结构的向量。 以下所有代码都包含在名为TD_Dijkstra_OS
的类中STRUCT:
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 *'到'const value_type&amp; {aka const TD_Dijkstra_OS&gt; :: instFunDescLeg&amp;}'
有人可以帮我实现插入程序吗?
答案 0 :(得分:2)
std::vector<struct instFunDescLeg> bp;
应写为std::vector<instFunDescLeg> bp;
你的结构非常简单,所以我认为没有必要使用你提供的内容存储一个指针向量。如果有一些隐藏的要求会强制您使用指针,请尝试将它们隐藏在智能指针类后面(例如unique_ptr
)。