2

Hi I know this seems like a really dumb question and honestly I'm lost on what is causing this object error and could really use some help.

I have this class here :

class PriorityQueue {
  public:
    PriorityQueue(std::size_t max_nodes);
    void insert(Key k);
    void insert(KeyValuePair kv);
    KeyValuePair min();
    KeyValuePair removeMin();
    bool isEmpty() const;
    size_t size() const;
    nlohmann::json JSON() const;

  private:
    void heapifyUp(size_t i);
    void heapifyDown(size_t i);
    void removeNode(size_t i);
    Key getKey(size_t i);

    std::vector<KeyValuePair>   nodes_;
    size_t                      max_size_;
    size_t                      size_;

    const static size_t         ROOT = 1;
};  // class PriorityQueue

#endif  // _PRIORITYQUEUE_H_

Everything is properly defined in the corresponding cpp file for it. Now I'm trying to call it in a separate file that includes both as a header.

#include "priorityqueue.cpp"
#include "priorityqueue.h"

But in my main() function when I try to call the class to an object like

PriorityQueue m;

I get the error

no matching function for call to ‘PriorityQueue::PriorityQueue()’
PriorityQueue m;

I know this seems like a really basic C++ question but I have no idea what I am doing wrong. Any help would be greatly appreciated.

1
  • 7
    #include "priorityqueue.cpp" - This is only going to cause you grief down the line. Get out of the habit. It's better to work with the way C++ is compiled and linked. Commented Feb 25, 2019 at 6:49

1 Answer 1

5

At the first, remove #include "priorityqueue.cpp" and just use of #include "priorityqueue.h"

Because your constructor has a parameter :

PriorityQueue(std::size_t max_nodes);

So, you should set max_nodes when you creating an object :

PriorityQueue m(10);

But you can also implement a default constructor with a default parameter :

In the header file

// In this approach you can use a default constructor
// Declaration
class PriorityQueue {
  public:
    PriorityQueue(std::size_t max_nodes = 10);
    ...
};

In the cpp file

// Implementation
PriorityQueue::PriorityQueue(std::size_t max_nodes)
{
    max_size_ = max_nodes;
 // initialize members
}

Then you can create instance like bellow :

PriorityQueue m;      // max_size_ will be set to default value (10)
PriorityQueue n(7);   // max_size_ will be set to 7

Try online

Sign up to request clarification or add additional context in comments.

4 Comments

Yep, now I realize it lol. This was extremely helpful, thank you!
To avoid redundancy, you can also set a default argument for max_nodes parameter as follows: PriorityQueue(std::size_t max_nodes = /* default */ ) .... Then, it will be a default constructor as well.
@MohammadrezaPanahi You didn't understand my comment. You still have 2 distinct constructors. My point was that both these constructors may be easily merged into one. BTW, it's better to initialize member variables in a constructor initializer list than in constructor's body.
It's worth stressing however that the default argument better appear at the class declaration to be of any use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.