Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to write output function for template classes?

Reply
Thread Tools

How to write output function for template classes?

 
 
Jayden Shui
Guest
Posts: n/a
 
      11-16-2011
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-16-2011
On 11/16/2011 12:54 PM, Jayden Shui wrote:
> 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:stream &operator<<(std: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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Jayden Shui
Guest
Posts: n/a
 
      11-16-2011
On Nov 16, 1:19*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 11/16/2011 12:54 PM, Jayden Shui wrote:
>
>
>
>
>
>
>
>
>
> > 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:stream &operator<<(std: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
> --
> I do not respond to top-posted replies, please don't ask


Thank you very much! It solved my question.

Best regards,

Jayden
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments