I have the following code:
#include <iostream> #include <stdio.h> using namespace std; template <class F> struct CMPLX { F Re, Im; struct _printnice { F Re, Im; string sep; _printnice(const F& Re, const F& Im, const string& sep) : Re(Re), Im(Im), sep(sep) {} }; CMPLX <F> (F Re, F Im) : Re(Re), Im(Im) {} _printnice PrintNice(const string& sep="\t"){ return _printnice(Re, Im, sep); } }; template<class F> ostream& operator << (ostream& os, const CMPLX<F> c){ cout << c.Re << " + " << c.Im << "i"; } template<class F> ostream& operator << (ostream& os, const CMPLX<F> :: _printnice p){ cout << p.Re << p.sep << p.Im; } int main(){ CMPLX<float> c(2.0,1.0); cout << c << endl; cout << c.PrintNice() << endl; }
I present the _printnice substructure in order to overload the << operator and have a result with a different format for my CMPLX class. However, this causes the expected unqualified-id before 'p to fail, and I don't know how to solve this (my knowledge of templates is very limited).
I am trying to change the second definition of << to the following, which works, but I have to specify the type that frowned:
ostream& operator << (ostream& os, const CMPLX <float> :: _printnice p){ cout << p.Re << p.sep << p.Im; }
source share