开发者

how to insert pair<int, int> into queue?

开发者 https://www.devze.com 2023-02-24 00:21 出处:网络
i am having trouble inserting objects of type pair<int, int> into a queue. i am getting a strange error, and i have no idea how to go about fixing it. Can anyone help? the following is the code

i am having trouble inserting objects of type pair<int, int> into a queue. i am getting a strange error, and i have no idea how to go about fixing it. Can anyone help? the following is the code for the method, followed by the error messages. The first two errors are for the insert, the last is for the usage of the operator=, help with that too would be appreciated. Thanks!

pair<int,int>* bfSpanningTree(int theVertex)
{
    queue< pair<int,int> > pairq;
    queue<int> nodeq;
    if(linkedAdjacencyList[theVertex]->value == theVertex && linkedAdjacencyList[theVertex]->adj != NULL)
    {
        Node* whereto;
        whereto = linkedAdjacencyList[theVertex]->adj;
        while(whereto->adj != NULL)
        {
            pairq.push(pair< &whereto->value, &whereto->adj->value >);
            nodeq.push(whereto->value);
            whereto = whereto->adj;
        }
        while(!nodeq.empty())
        {
            whereto = linkedAdjacencyList[theVertex]->adj;
            while(whereto->adj != NULL)
            {
                pairq.push(pair<&whereto->value, &whereto->adj->value>);
                whereto = whereto->adj;
            }
        }
    }
    int i = 0;
    pair<int,int>* retVal;
    pair<int,int> tree[pairq.size()];
    while(!pairq.empty())
    {
        tree[i] = pairq.pop();
        i++;
    }
    retVal = tree;
    return retVal;
}

~UndirectedGraph()
{
    for (int i = 0; i < numVerticies; i++)
        delete[] linkedAdjacencyList[i];
}

errors:

hw8.h:181: error: wrong number of template arguments (1, should be 2)

/usr/include/c++/4.4/bits/stl_pair.h:67: error: provided for ‘template<class _T1, class _T2> struct std::pair’

hw8.h:190: error: wrong number of template arguments (1, should be 2)

/usr/include/c++/4.4/bits/stl_pair.h:67: error: provided for ‘template<class _T1, class _T2> struct std::pair’

hw8.h:200: error: no match for ‘operator=’ in ‘tree[i] = pairq.std::queue<_Tp, _Sequence>::pop [with _Tp = std::pair<int, int>, _Sequence = std::deque<std::pair&l开发者_如何学运维t;int, int>, std::allocator<std::pair<int, int> > >]()’

/usr/include/c++/4.4/bits/stl_pair.h:68: note: candidates are: std::pair<int, int>& std::pair<int, int>::operator=(const std::pair<int, int>&)


Lines of code like so:

pairq.push(pair< &whereto->value, &whereto->adj->value >);

should probably look like:

pairq.push(make_pair(whereto->value, whereto->adj->value));

or if the value members aren't of type int:

pairq.push(pair<int,int>(whereto->value, whereto->adj->value));

Finally, queue::pop() doesn't return anything, so you probably want:

tree[i] = pairq.front();
pairq.pop();


Take a look at make_pair(). queue::pop() doesn't return the first element. You need the following:

tree[i] = pairq.front();
pairq.pop();


You can't make a template from actual object instantiations like you've done ... they must be TYPES for the objects being instantiated by the template function (in this case the constructor).

So for instance, you can make a pair object using a constructor like:

pair<int, int>(whereto->value, whereto->adj->value)

or you can make a pair using the utility function make_pair() as shown by Michael.

But if you're going to use the constructor, you have to somewhere declare the types that will replace the types T1 and T2 in the constructor's declaration, i.e.,

template<typename T1, typename T2>
pair::pair(const T1& object_1, const T2& object_2);

That is done by declaring the object with the template arguments for the desired object types (i.e, for a pair of int objects, you would use pair<int, int>), and then calling the actual object member function with objects of those types (in your case it would be a constructor for class pair).


There's a few things going on here at first glance - can you provide the Node class/struct definition?

  • You're returning a pointer to an automatic (stack) element at the end, which will be going out of scope. You'll want to return by value, most likely (alternatively, allocate and return a pointer, preferably shared, to the element)
  • queue.pop() returns void - see http://www.cplusplus.com/reference/stl/queue/pop/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号