How to write output function for template classes?

J

Jayden Shui

Hello All,

I have a code like:

template<class T, char open, char close>
class Enclosed
{
public:
Enclosed(T& t) : mT(t) {}

friend ostream& operator<<(ostream& os, Enclosed const& e);

private:
T& mT;
};

template<class T, char open, char close>
ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
{
os << open << e.mT << close;
return os;
}

When I test it by

cout << Enclosed<int, '[', ']'>(5);

The compiler said they can not find the operator << function. Would
you please help me to look into it?

Thank you for your kind help!

Best regards,

Jayden
 
V

Victor Bazarov

Hello All,

I have a code like:

template<class T, char open, char close>
class Enclosed
{
public:
Enclosed(T& t) : mT(t) {}

friend ostream& operator<<(ostream& os, Enclosed const& e);

private:
T& mT;
};

template<class T, char open, char close>
ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
{
os<< open<< e.mT<< close;
return os;
}

When I test it by

cout<< Enclosed<int, '[', ']'>(5);

The compiler said they can not find the operator<< function. Would
you please help me to look into it?

-------------------------------------------- >8 cut here
#include <iostream>
using namespace std;

template<class T, char open, char close>
class Enclosed
{
public:
Enclosed(T& t) : mT(t) {}

friend ostream& operator<<(ostream& os, Enclosed const& e);

private:
T& mT;
};

template<class T, char open, char close>
ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
{
os << open << e.mT << close;
return os;
}

int main()
{
cout << Enclosed<int, '[', ']'>(5);
}
-------------------------------------------- >8 cut here

Comeau says

"ComeauTest.c", line 11: warning: "std::eek:stream &operator<<(std::eek:stream
&, const
Enclosed<T, open, close> &)" declares a non-template function
-- add
<> to refer to a template instance
friend ostream& operator<<(ostream& os, Enclosed const& e);
^

"ComeauTest.c", line 26: error: no instance of constructor "Enclosed<T,
open,
close>::Enclosed [with T=int, open='[', close=']']" matches the
argument list
The argument types that you used are: (int)
cout << Enclosed<int, '[', ']'>(5);
^

You declare a non-template operator<< as the friend of your class
template and then you define a template function... See FAQ 35.16.

BTW, you need to make your Enclosed take a 'const T&' if you want to use
it with the literal like '5'.

V
 
J

Jayden Shui

Hello All,
I have a code like:
template<class T, char open, char close>
class Enclosed
{
public:
     Enclosed(T&  t) : mT(t) {}
     friend ostream&  operator<<(ostream&  os, Enclosed const&  e);
private:
     T&  mT;
};
template<class T, char open, char close>
ostream&  operator<<(ostream&  os, Enclosed<T, open, close>  const&  e)
{
     os<<  open<<  e.mT<<  close;
     return os;
}
When I test it by
cout<<  Enclosed<int, '[', ']'>(5);
The compiler said they can not find the operator<<  function. Would
you please help me to look into it?

-------------------------------------------- >8 cut here
#include <iostream>
using namespace std;

template<class T, char open, char close>
class Enclosed
{
public:
     Enclosed(T& t) : mT(t) {}

     friend ostream& operator<<(ostream& os, Enclosed const& e);

private:
     T& mT;

};

template<class T, char open, char close>
ostream& operator<<(ostream& os, Enclosed<T, open, close> const& e)
{
     os << open << e.mT << close;
     return os;

}

int main()
{
    cout << Enclosed<int, '[', ']'>(5);}

-------------------------------------------- >8 cut here

Comeau says

"ComeauTest.c", line 11: warning: "std::eek:stream &operator<<(std::eek:stream
&, const
           Enclosed<T, open, close> &)" declares a non-template function
-- add
           <> to refer to a template instance
       friend ostream& operator<<(ostream& os, Enclosed const& e);
                       ^

"ComeauTest.c", line 26: error: no instance of constructor "Enclosed<T,
open,
           close>::Enclosed [with T=int, open='[', close=']']" matches the
           argument list
             The argument types that you used are: (int)
      cout << Enclosed<int, '[', ']'>(5);
              ^

You declare a non-template operator<< as the friend of your class
template and then you define a template function...  See FAQ 35.16.

BTW, you need to make your Enclosed take a 'const T&' if you want to use
it with the literal like '5'.

V

Thank you very much! It solved my question.

Best regards,

Jayden
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top