std::pair in ofstream speichern



  • Hallo

    Wie kann man dies relisieren:

    ofstream m_FiletoWriteIn;
    std::pair<std::string, int > m_PairHighscore;
    m_FileToWriteIn<<m_pairHighscore<<std::endl;
    

    Ich bekomme immer die Fehlermeldung:

    error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::pair<_Ty1,_Ty2>' (or there is no acceptable conversion)
    with
    [
    _Ty1=std::string,
    _Ty2=int
    ]

    Danke für eure Hilfe

    chrische



  • Hallo,

    Machs halt nacheinander.

    m_FileToWriteIn << m_pairHighscore.first << " " << m_pairHighscore.second << std::endl;
    


  • du könntest dir eventuell auch den operator<< überladen für pair ganz allgemein

    template <class T, class U>
    std::ostream& operator<< (std::ostream& out, std::pair<T,U> const& p)
    {
       return out << p.first << ' ' << p.second; //hier kommt es drauf an, wie du die daten letztendlich dann ausgegeben haben möchtest
    }
    

Anmelden zum Antworten