mercredi 16 décembre 2020

No suitable user-defined conversion from

I am new to C++. I was trying to create a class similar to multimap, called multimap_viewer. I would like to be able to convert my multimap_viewer to a multimap, but I just don't know how. I have tried to write an assignment operator, but it's not working. Could someone point me out in the right direction, please?

Code to demonstrate the error:

multimap_viewer<std::string, std::string> m_view;
std::map<std::string, std::string> mymap;
mymap[ "this" ] = "that";
m_view.add( mymap );

std::multimap<std::string, std::string, string_size_less> a = m_view;

The error I receiving in the last line is:

no suitable user-defined conversion from "multimap_viewer<std::string, std::string>" to "std::multimap<std::string, std::string, string_size_less, std::allocator<std::pair<const std::string, std::string>>>" exists

My wannabe working assignment operator:

template <class T, class V>
class multimap_viewer
{
  public: 
  std::multimap<T, V, class Comp, std::allocator<std::pair<const T, V>>>& operator=(multimap_viewer<T, V> mv)
  {
    std::multimap<T,V, class Comp, std::allocator<std::pair<const T, V>>> mp;
    // copy items and stuff
    return *mp;
  }
}

Aucun commentaire:

Enregistrer un commentaire