C++ 如何通过引用传递对象以供其他文件使用
我想在名为MinPriority.cpp的文件中访问此函数:C++ 如何通过引用传递对象以供其他文件使用,c++,oop,parameter-passing,pass-by-reference,C++,Oop,Parameter Passing,Pass By Reference,我想在名为MinPriority.cpp的文件中访问此函数: MinPriority::priority& MinPriority::HEAP_EXTRACT_MIN(*this) //dequeue's min element of tree { MinPriority::priority& min = heapArray.front(); heapArray.erase(heapArray.begin()); MIN_HEAPIFY(0); r
MinPriority::priority& MinPriority::HEAP_EXTRACT_MIN(*this) //dequeue's min element of tree
{
MinPriority::priority& min = heapArray.front();
heapArray.erase(heapArray.begin());
MIN_HEAPIFY(0);
return min;
}
它应该将一个元素从向量heapArray
中出列,并将对象优先级
传递到另一个名为Graph.cpp的文件中。这里是在Graph.cpp中调用函数的地方
void Graph::MST_PRIM()
{
MinPriority priority;
for(unsigned int i = 0; i != adjList.size(); i++)
{
priority.createArray(adjList[i].front().m_vertex, *this);
}
while(priority.queue_Empty() == false)
{
priority& u = priority.HEAP_EXTRACT_MIN(/*...*/); //graph.cpp 88
}
}
MinPriority类声明为:(为了简单起见,将其略图)
不确定您正试图使用此代码执行什么操作:
MinPriority::priority& MinPriority::HEAP_EXTRACT_MIN(*this) //dequeue's min element of tree
{
MinPriority::priority& min = heapArray.front();
heapArray.erase(heapArray.begin());
MIN_HEAPIFY(0);
return min;
}
但这里有几个问题:
MinPriority::priority MinPriority::PopFront()
{
// Copy of the front element
MinPriority::priority min = heapArray.front();
// Remove/destroy the front element
heapArray.erase(heapArray.begin());
// Return the copy
return min;
}
为什么在
HEAP\u EXTRACT\u MIN
参数列表中有*这个
?它在另一个函数中工作,我在这篇文章中得到了帮助:它传递给一个函数,而不是在函数声明中。你到底有什么问题?你的错误是什么?我会把它贴在上面。。。已编辑。优先级和堆提取最小值(优先级和最小值);这是MinPriority.h:39错误发生的声明是的,我试过这么做,我遇到的问题实际上是访问函数(我问这个问题的原因)。请参阅我问题中的图表。cpp:88。。。如何正确访问PopFront()返回的内容?g++-Wall-W-pedantic-g-c Graph.cpp Graph.cpp:In member functionvoid Graph::MST_PRIM()':Graph.cpp:88:错误:预期
;'在“u”图形之前.cpp:88:警告:语句无效我尝试了MinPriority::priority u=priority.HEAP\u EXTRACT\u MIN()我要看看它是否有效。
MinPriority::priority& MinPriority::HEAP_EXTRACT_MIN(*this) //dequeue's min element of tree
{
MinPriority::priority& min = heapArray.front();
heapArray.erase(heapArray.begin());
MIN_HEAPIFY(0);
return min;
}
MinPriority::priority MinPriority::PopFront()
{
// Copy of the front element
MinPriority::priority min = heapArray.front();
// Remove/destroy the front element
heapArray.erase(heapArray.begin());
// Return the copy
return min;
}