我在将类型的对象插入pair<int, int>队列时遇到问题.我收到一个奇怪的错误,我不知道如何修复它.有人可以帮忙吗?以下是该方法的代码,后跟错误消息.前两个错误是针对插入,最后一个是用于使用operator=,帮助也是值得赞赏的.谢谢!
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];
}
Run Code Online (Sandbox Code Playgroud)
错误:
hw8.h:181:错误:模板参数数量错误(1,应为2)
/usr/include/c++/4.4/bits/stl_pair.h:67:错误:提供
‘template<class _T1, class _T2> struct std::pair’hw8.h:190:错误:模板参数数量错误(1,应为2)
/usr/include/c++/4.4/bits/stl_pair.h:67:错误:提供
‘template<class _T1, class _T2> struct std::pair’hw8.h:200:错误:不匹配
‘operator=’in‘tree[i] = pairq.std::queue<_Tp, _Sequence>::pop [with _Tp = std::pair<int, int>, _Sequence = std::deque<std::pair<int, int>, std::allocator<std::pair<int, int> > >]()’/usr/include/c++/4.4/bits/stl_pair.h:68:注意:候选人是:
std::pair<int, int>& std::pair<int, int>::operator=(const std::pair<int, int>&)
像这样的代码行:
pairq.push(pair< &whereto->value, &whereto->adj->value >);
Run Code Online (Sandbox Code Playgroud)
应该看起来像:
pairq.push(make_pair(whereto->value, whereto->adj->value));
Run Code Online (Sandbox Code Playgroud)
或者如果value成员不是类型int:
pairq.push(pair<int,int>(whereto->value, whereto->adj->value));
Run Code Online (Sandbox Code Playgroud)
最后,queue::pop()不返回任何内容,所以你可能想要:
tree[i] = pairq.front();
pairq.pop();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26063 次 |
| 最近记录: |