0

I have the following code and it doesn't compile when I create an object with an empty constructor like so:

PriorityQueue pq1();
pq1.insert(3); // doesn't compile

But it does compile like this:

PriorityQueue pq2 = PriorityQueue();
pq2.insert(3); // compiles

Why does that happen ?

PriorityQueue.h

class PriorityQueue
{
public:
    PriorityQueue();  // Create the heap
    ~PriorityQueue(); // Destroy the heap
}

PriorityQueue.cpp

#include "PriorityQueue.h"

PriorityQueue::PriorityQueue()
{

}

main.cpp

#include "PriorityQueue.h"

int main()
{
    PriorityQueue pq1();
    pq1.insert(3); // doesn't compile

    PriorityQueue pq2 = PriorityQueue();
    pq2.insert(3); // compiles
}
dimitris93
  • 4,155
  • 11
  • 50
  • 86

1 Answers1

4
PriorityQueue pq1();

The above statement doesn't create an object. It is a function declaration of pq1 whose return type is PriorityQueue.

PriorityQueue pq1; // Remove ()

Most Vexing parse

Mahesh
  • 34,573
  • 20
  • 89
  • 115