Jump to content
  • Advertisement
Sign in to follow this  
FGFS

overload << operator // multimap

This topic is 1819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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;
}
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!