简体   繁体   English

将动态创建的struct项插入到struct的向量中

[英]Insert a dynamically created struct item into a vector of structs

I'm trying to fill a vector of structs with dynamically created struct elements. 我正在尝试用动态创建的struct元素填充结构的向量。 All of the code below is contained into a class named TD_Dijkstra_OS 下面的所有代码都包含在名为TD_Dijkstra_OS的类中

Struct: 结构:

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

I compute the values needed to assign in the item's fiels into a function as: 我计算将项目的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);

and then i create a struct item and try to insert it to a vector of instFunDescLeg items, declared as: 然后我创建一个struct项并尝试将其插入到instFunDescLeg项的向量中,声明为:

  std::vector<struct instFunDescLeg> bp;

into this function: 进入这个功能:

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

When I compile this, I get an error message which says that there's no function matching bp.push_back(leg). 当我编译它时,我收到一条错误消息,指出没有匹配bp.push_back(leg)的函数。 A note after the error message sheds some light to this situation: 错误消息后的注释说明了这种情况:

 /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&}' :: instFunDescLeg *'to'const value_type&{aka const TD_Dijkstra_OS> :: instFunDescLeg&}'

Can someone help me with the implementation of the insert procedure? 有人可以帮我实现插入程序吗?

std::vector<struct instFunDescLeg> bp; should be written std::vector<instFunDescLeg> bp; 应写成std::vector<instFunDescLeg> bp;

Your struct is fairly simple, so I see no need to store a vector of pointers to it with what you are provided. 你的结构非常简单,所以我认为没有必要使用你提供的内容存储一个指针向量。 If there is some hidden requirement that would force you to use pointers, try to hide them behind smart pointer classes (eg unique_ptr ). 如果有一些隐藏的要求会强制您使用指针,请尝试将它们隐藏在智能指针类后面(例如unique_ptr )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM