overload << operator // multimap

Started by
4 comments, last by Paradigm Shifter 10 years, 8 months ago

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

Advertisement

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;
}
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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&’

...

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement