gpt4 book ai didi

c++ - 将动态创建的结构项插入到结构 vector 中

转载 作者:行者123 更新时间:2023-11-28 03:10:04 25 4
gpt4 key购买 nike

我正在尝试用动态创建的结构元素填充结构 vector 。以下所有代码都包含在名为 TD_Dijkstra_OS 的类中

结构:

struct instFunDescLeg
{
float Ap, Bp, tfail;
typename GraphType::NodeIterator n; //external library data type
};

我将项目字段中分配给函数的值计算为:

           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);

然后我创建一个结构项并尝试将它插入到 instFunDescLeg 项的 vector 中,声明为:

  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;

您的结构相当简单,所以我认为没有必要使用您提供的内容来存储指向它的指针 vector 。如果有一些隐藏的要求会迫使您使用指针,请尝试将它们隐藏在智能指针类后面(例如 unique_ptr )。

关于c++ - 将动态创建的结构项插入到结构 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com