OVH Cloud OVH Cloud

std::endl type unknown

2 réponses
Avatar
Xav
#include <iostream>

using namespace std;

class CLog
{
public:
template<typename paramT>
CLog& operator<<(const paramT& param)
{
return *this;
};
};

int main()
{
CLog Log;

Log << endl; // no match for `CLog& << <unknown type>' operator
}

Pourquoi le type de endl est-il inconnu ?
Comment puis-je passer endl a l'operator<< ?

2 réponses

Avatar
Loïc Joly
Xav wrote:

#include <iostream>

Pourquoi le type de endl est-il inconnu ?


std::endl est défini dans <ostream>.

--
Loïc

Avatar
Benoit Rousseau
Xav wrote:
#include <iostream>

using namespace std;

class CLog
{
public:
template<typename paramT>
CLog& operator<<(const paramT& param)
{
return *this;
};
};

int main()
{
CLog Log;

Log << endl; // no match for `CLog& << <unknown type>' operator
}

Pourquoi le type de endl est-il inconnu ?
Comment puis-je passer endl a l'operator<< ?


std::endl est un manipulateur d'ostream, et ne marche qu'avec des

std::ostream. Il faut faire dériver CLog d'un ostream
la fonction correspondant à endl ressemble à (template en moins)
ostream& endl( ostream& __os ) {
return flush(__os.put(__os.widen('n')));
}

Il faudra que tu crée un manipulateur du même style si tu ne veux pas
dériver ostream.

--------------------------------------------
Benoît Rousseau : roussebe at spray dot se
Jouez en programmant : http://realtimebattle.sourceforge.net/