Sign in to follow this  
FGFS

overload << operator // multimap

Recommended Posts

Hi

 with:

    for (std::multimap<string, ProcedureList>::iterator it=m_procedure_map.begin(); it!=m_procedure_map.end(); ++it)
        std::cout << (*it).first << " => " << (*it).second << '\n';
      //  XLMDebugString((*it).second);

I get:

../FS/IFSDatabase.cpp:572:51: error: no match for ‘operator<<’ in ‘std::operator<< <std::char_traits<char> >((* & std::operator<< <char, std::char_traits<char>, std::allocator<char> >((* & std::cout), (* & it.std::_Rb_tree_iterator<_Tp>::operator*<std::pair<const std::basic_string<char>, std::vector<Procedure*> > >().std::pair<const std::basic_string<char>, std::vector<Procedure*> >::first))), ((const char*)" => ")) << it.std::_Rb_tree_iterator<_Tp>::operator*<std::pair<const std::basic_string<char>, std::vector<Procedure*> > >().std::pair<const std::basic_string<char>, std::vector<Procedure*> >::second’

 

Now how to overload << as ProcedureList is a list of vector pointers. Or convert to char* for use with //  XLMDebugString.

Thanks

 

 

Share this post


Link to post
Share on other sites

something like:

 

EDIT: assuming ProcedureList is a typedef for a list<vector*>, if the list<vector*> is a member you need to use myList.theMemberThatIsAList.begin(), etc. instead.

 

You may have to declare ostream& operator<<(ostream&, const ProcedureList&) a friend of ProcedureList if you need to access private members (or write public accessors).

ostream& operator<<(ostream& os, const ProcedureList& myList)
{
   // iterate over the list
   for(auto it = myList.begin(); it != myList.end(); ++it)
   {
      // output something signifying beginning of each list
      os << "{ ";

      // iterate over the vector, your list contains pointers to vectors?
      for(auto itVec = (*it)->begin(); itVec != (*it)->end(); ++itVec)
      {
         os << *itVec << " "; // output element
      }

      os << "}\n";
   }

   return os;
}
Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

Thanks but I don't understand... wacko.png not my code.

 

Basically I want to see the values.

 

Declarations are:

typedef vector<Procedure*> ProcedureList;

ProcedureMap m_procedure_map;

typedef multimap<string, ProcedureList> ProcedureMap;

 

I've tried something alike:

    friend ostream& operator<<(ostream& os, const std::multimap<string, ProcedureList>& m_procedure_map);

in my class withou success. Anyway and how to invoke it?

Thanks again

Share this post


Link to post
Share on other sites

My code is a solution to

 

for (std::multimap<string, ProcedureList>::iterator it=m_procedure_map.begin(); it!=m_procedure_map.end(); ++it)
        std::cout << (*it).first << " => " << (*it).second << '\n';
 

failing to compile (it provides the function which implements cout << (*it).second).

 

I've not tested it though it should work?

Share this post


Link to post
Share on other sites

It doesn't compile. I get errors:

 

/usr/include/qt5/QtCore/qdatastream.h:246:14: note: template<class T> QDataStream& operator<<(QDataStream&, const QList<T>&)
/usr/include/qt5/QtCore/qdatastream.h:246:14: note:   template argument deduction/substitution failed:
../FS/MDatabase.cpp:572:50: note:   cannot convert ‘std::operator<< <std::char_traits<char> >((* & std::operator<< <char, std::char_traits<char>, std::allocator<char> >((* & std::cout), (* & it.std::_Rb_tree_iterator<_Tp>::operator*<std::pair<const std::basic_string<char>, std::vector<Procedure*> > >().std::pair<const std::basic_string<char>, std::vector<Procedure*> >::first))), ((const char*)" => "))’ (type ‘std::basic_ostream<char>’) to type ‘QDataStream&’

...

Share this post


Link to post
Share on other sites

Oh right yeah, the (*it).second in the multimap is a container as well, that may cause problems, you need to iterate all the elements of the multimap keyed by (*it).first?

 

And I dunno where QDataStream comes from I thought you were using std::cout (which is a std::ostream?).

 

The operator<< doesn't want to be a class member, it needs to be a free function (i.e. not defined in a class) but you may need to friend it inside the class (which just acts as a declaration of the free function). I can't tell if you tried to define it inside the class or not, so ignore that comment if you already have it as a free function.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this