473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Friends of Templates

Hi,

Can someone please tell me the correct syntax (if its possible of course)
to specify an output stream operator for a templated class so that I dont
need to write the same function for all the derived classes from the
template. I have a sample of what it thought it might look like below.
==BEGIN CODE SNIPPET===

template<class T1, class T2>
class MyDerived : public MyClass {
friend ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa);
....
};

template<class T1, class T2>
ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa) {
}

===END CODE SNIPPED====

Thanks in advance for any suggestions.

Cheers

Tom

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #1
1 1316
"Tom McCallum" <te********@hotmail.com> wrote in message
news:op**************@news.blueyonder.co.uk...
Hi,

Can someone please tell me the correct syntax (if its possible of course)
to specify an output stream operator for a templated class so that I dont
need to write the same function for all the derived classes from the
template. I have a sample of what it thought it might look like below.
==BEGIN CODE SNIPPET===

template<class T1, class T2>
class MyDerived : public MyClass {
friend ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa);
...
};

template<class T1, class T2>
ostream& operator<< (ostream& os, MyDerived<T1, T2> * sa) {
}

===END CODE SNIPPED====

Thanks in advance for any suggestions.


Not sure why the above code takes a pointer instead of the usual const
reference.

This is covered in the FAQ (http://www.parashift.com/c++-faq-lite/). See
section 34 ("Container classes and templates"), question 15 ("Why do I get
linker errors when I use template friends?").

--
David Hilsee
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gianni Mariani | last post by:
I have 2 distinct template classes which co-operate, hence are friends. However, I can't seem to figure out what syntax to use to make this work. What is the right(tm) way to write a friend...
6
by: Roger Leigh | last post by:
I've written a fixed-precision class, "numeric". This stores the number of decimal places as a template parameter. I've overloaded all of the normal numerical operators (an example): ...
10
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
7
by: fabio de francesco | last post by:
Hello, I'm just joking with the Subject, but I really don't know how to make a synthesis of two questions about some code I'm trying to write. In the following I post this little code, with...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
9
by: Klaas Vantournhout | last post by:
Hi all, I have a question about friends functions of a template class. To make it simple, I would like to do something like this. Assume that I have a class foo with template T ...
6
by: Andrey Tarasevich | last post by:
Hello Consider the following code fragment namespace N { struct S { void foo(); }; }
4
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; ...
5
by: emboss | last post by:
If templates worked how I would like them to work, I'd be able to do the following: template <int N> class A { private: int X; public: template <int M> void foo(const A<M>...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.