1

I am trying to better familiarize myself with C++ template programming. For practice I decided to implement a Priority Queue Dictionary template class with an underlying priority_queue and unordered_map. However, when I try to compile my program with clang++ on my MBP, it prints out some error messages that I do not understand.

My code is, in a file called pqDictTotal.cpp,

#include <queue>
#include <unordered_map>
#include <string>
#include <iostream>

template <typename P, typename V>
class PQDict {

private:

  std::priority_queue<P> *pq;
  std::unordered_map<P, V> *m;

public:

  int getSize() {

    return this->pq->size();
  }

  bool isEmpty() {

    return this->pq->empty();
  }

  V getMin() {

    P priority = this->pq->top();
    V value = this->m[priority];

    return value;
  }

  P getMinPriority() {

    return this->pq->top();
  }

  V removeMin() {

    P priority = this->pq->pop();
    V value = this->m[priority];
    this->m->erase(priority);

    return value;
  }

  void insert(P priority, V value) {

    this->pq->push(priority);
    this->m[priority] = value;
  }

  PQDict() {

    this->pq = new std::priority_queue<P>();
    this->m = new std::unordered_map<P, V>();
  }

  ~PQDict() {

    delete pq;
    delete m;
  }
};

int main() {

  PQDict<int, std::string> mypq;

  mypq.insert(23, "BDWF");
  mypq.insert(0, "ABC");
  mypq.insert(15, "SDFSDFL");
  mypq.insert(3, "GHF");
  mypq.insert(10, "LKSJDF");

  int minp;
  std::string mins;

  while (!mypq.isEmpty()) {

    minp = mypq.getMinPriority();
    mins = mypq.removeMin();

    std::cout << minp << ", " << mins << std::endl;
  }
}

When compiled with

clang++ pqDictTotal.cpp

I get

pqDictTotal.cpp:51:23: error: no viable overloaded '='
    this->m[priority] = value;
    ~~~~~~~~~~~~~~~~~ ^ ~~~~~
pqDictTotal.cpp:71:8: note: in instantiation of member function 'PQDict<int,
      std::__1::basic_string<char> >::insert' requested here
  mypq.insert(23, "BDWF");
       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:829:20: note: 
      candidate function not viable: no known conversion from
      'std::__1::basic_string<char>' to 'const std::__1::unordered_map<int,
      std::__1::basic_string<char>, std::__1::hash<int>,
      std::__1::equal_to<int>, std::__1::allocator<std::__1::pair<const int,
      std::__1::basic_string<char> > > >' for 1st argument
    unordered_map& operator=(const unordered_map& __u)
                   ^
pqDictTotal.cpp:41:7: error: cannot initialize a variable of type 'int' with an
      rvalue of type 'void'
    P priority = this->pq->pop();
      ^          ~~~~~~~~~~~~~~~
pqDictTotal.cpp:83:17: note: in instantiation of member function 'PQDict<int,
      std::__1::basic_string<char> >::removeMin' requested here
    mins = mypq.removeMin();
                ^
2 errors generated.

Why would I need to overload = for unordered_map? What does it mean to 'initialize a variable of type 'int' with an rvalue of type 'void'?

Any help would be greatly appreciated.

Teo Gelles
  • 82
  • 5
  • 3
    It means this `PQDict mypq();` is a function declaration. You need `PQDict mypq;` or `PQDict mypq{};` – juanchopanza Jan 12 '15 at 21:14
  • Many thanks for the quick response. I have edited the question to address the incorrect constructor syntax. There remain two errors which I do not understand. If anyone has any feedback on these problems, please let me know. – Teo Gelles Jan 13 '15 at 06:50
  • You should ask one separate question for each error, with an [MCVE](http://stackoverflow.com/help/mcve). – juanchopanza Jan 13 '15 at 06:54

0 Answers0