C++ 如何插入配对<;int,int>;排队?

C++ 如何插入配对<;int,int>;排队?,c++,templates,C++,Templates,我无法将类型为pair的对象插入队列。我遇到了一个奇怪的错误,我不知道如何着手修复它。有人能帮忙吗?下面是该方法的代码,后面是错误消息。前两个错误用于插入,最后一个错误用于使用运算符=,请提供帮助。谢谢 pair<int,int>* bfSpanningTree(int theVertex) { queue< pair<int,int> > pairq; queue<int> nodeq; if(linkedAdjacenc

我无法将类型为
pair
的对象插入队列。我遇到了一个奇怪的错误,我不知道如何着手修复它。有人能帮忙吗?下面是该方法的代码,后面是错误消息。前两个错误用于插入,最后一个错误用于使用
运算符=
,请提供帮助。谢谢

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];
}
pair*bfSpanningTree(顶点内)
{
队列pairq;
队列节点;
if(linkedAjacyList[theVertex]->value==theVertex&&linkedAjacyList[theVertex]->adj!=NULL)
{
节点*指向何处;
其中to=LinkedAjacyList[theVertex]>adj;
while(whereto->adj!=NULL)
{
pairq.push(配对<&where-to->value,&where-to->adj->value>);
nodeq.push(到哪里->值);
whereto=whereto->adj;
}
而(!nodeq.empty())
{
其中to=LinkedAjacyList[theVertex]>adj;
while(whereto->adj!=NULL)
{
pairq.push(pairvalue,&whereto->adj->value>);
whereto=whereto->adj;
}
}
}
int i=0;
对*retVal;
对树[pairq.size()];
而(!pairq.empty())
{
tree[i]=pairq.pop();
i++;
}
retVal=树;
返回返回;
}
~UndirectedGraph()
{
对于(int i=0;i
错误:

hw8.h:181:错误:模板参数的数目错误(1,应该是2)

/usr/include/c++/4.4/bits/stl_pair.h:67:错误:为
'template struct std::pair'

h:190:错误:模板参数的数目错误(1,应该是2)

/usr/include/c++/4.4/bits/stl_pair.h:67:错误:为
'template struct std::pair'

hw8.h:200:错误:树[i]=pairq.std::queue::pop[with _Tp=std::pair,_Sequence=std::deque]()中的
'operator='
不匹配

/usr/include/c++/4.4/bits/stl_pair.h:68:注:候选项为:
std::pair&std::pair::operator=(const std::pair&)

这样的代码行:

pairq.push(pair< &whereto->value, &whereto->adj->value >);
或者如果
成员的类型不是
int

pairq.push(pair<int,int>(whereto->value, whereto->adj->value));
看一看。不返回第一个元素。您需要以下内容:

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

你不能像以前那样从实际的对象实例化中创建模板。。。它们必须是模板函数(在本例中为构造函数)实例化的对象的类型

例如,您可以使用以下构造函数创建pair对象:

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

或者您可以使用实用程序函数
make\u pair()
创建一对,如Michael所示

但是如果要使用构造函数,必须在某个地方声明将替换构造函数声明中类型
T1
T2
的类型,即

template<typename T1, typename T2>
pair::pair(const T1& object_1, const T2& object_2);
模板
pair::pair(常数T1和对象_1,常数T2和对象_2);

这是通过使用所需对象类型的模板参数声明对象来完成的(即,对于一对
int
对象,您将使用
pair
),然后使用这些类型的对象调用实际的对象成员函数(在您的情况下,它将是类
pair
的构造函数).

乍一看,这里有一些事情-您能提供节点类/结构定义吗

  • 您将在末尾返回指向自动(堆栈)元素的指针,该元素将超出范围。您很可能希望按值返回(或者,分配并返回指向元素的指针,最好是共享的)
  • pop()返回void-请参阅

哪一行代码位于
hw8.h
第181行?
tree[i] = pairq.front();
pairq.pop();
template<typename T1, typename T2>
pair::pair(const T1& object_1, const T2& object_2);