Question

I want to access this function in a file known as 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);
    return min;
}

It is supposed to dequeue an element from the vector heapArray and pass the object priority into another file known as Graph.cpp. Here is where the function is called in 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
    }

}

class MinPriority is declared as: (sketched it up for simplicity)

class MinPriority
{
private:
    class priority
    {
    public:
        priority(string preVertex, string vertex, int weight)
        {
            m_preVertex = preVertex;
            m_vertex = vertex;
            m_weight = weight;
        }
        ~priority(){}
        string m_preVertex;
        string m_vertex;
        int m_weight;
    };
    vector<priority> heapArray;
public:
        //member functions are included here...
};

basically, in the end, I want to be able to pop stuff out of heapArray, pass it to functions in Graph.cpp and possibly modify things already in the heapArray from Graph.cpp. I never need push things onto the data structure as they are already there (because of a function not shown). Error:

$ make -f makefile.txt
g++ -Wall -W -pedantic -g -c Graph.cpp
Graph.cpp: In member function `void Graph::MST_PRIM()':
Graph.cpp:88: error: `u' undeclared (first use this function)
Graph.cpp:88: error: (Each undeclared identifier is reported only once for each function it appears in.)
Graph.cpp:88: error: no matching function for call to `MinPriority::HEAP_EXTRACT_MIN()'
MinPriority.h:39: note: candidates are: MinPriority::priority& MinPriority::HEAP_EXTRACT_MIN(MinPriority::priority&)
Graph.cpp: At global scope:
Graph.cpp:97: warning: unused parameter 'vertex'
makefile.txt:9: recipe for target `Graph.o' failed
make: *** [Graph.o] Error 1
Was it helpful?

Solution

Not sure what you're trying to do with this code:

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

But you have a few issues here:

  1. The method has no argument type, it just has *this which makes no sense.
  2. You are returning a reference to a temporary, this is undefined behaviour and will likely crash.

I think what you actually wanted was a wrapper method to return the front of the deque and remove it:

 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;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top