473,804 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Define friend operator << for class template.

Hi,

I have a template class and I want to define an operator << as a friend
function. For each instantiation of the class I want a corresponding
instantiation of operator <<.
The following example fails to compile with g++ version 4.1.2.
I would appreciate it if you could help me fix it or point me to a suitable
reference.

Thank you,
Joe Hesse

*************** *************** **************
#include <iostream>

// forward declaration
template <typename T>
class Foo;

// forward declaration
template <typename T>
std::ostream & operator << (std::ostream &, const Foo<T&);

template <typename T>
class Foo {
private:
T value;
public:
Foo(const T & v) : value(v) {}
friend std::ostream & operator << <(std::ostrea m &, const Foo<T&);
};

// implement operator <<
template <typename T>
std::ostream & operator << (std::ostream &o, const Foo<T&f) {
return o << f.value ;
}

int main() {
Foo<intfi;
std::cout << fi;

return 0;
}

/* Here are the compiler error messages
Test.cpp: In function int main():
Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
Test.cpp:16: note: candidates are: Foo<T>::Foo(con st T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(c onst Foo<int>&)
*/

*************** *************** **************
Dec 6 '07 #1
2 1936
Please forgive me, I have an obvious error. No response is needed from the
newsgroup.

"Joe Hesse" <jo*******@actc x.comwrote in message
news:13******** *****@corp.supe rnews.com...
Hi,

I have a template class and I want to define an operator << as a friend
function. For each instantiation of the class I want a corresponding
instantiation of operator <<.
The following example fails to compile with g++ version 4.1.2.
I would appreciate it if you could help me fix it or point me to a
suitable reference.

Thank you,
Joe Hesse

*************** *************** **************
#include <iostream>

// forward declaration
template <typename T>
class Foo;

// forward declaration
template <typename T>
std::ostream & operator << (std::ostream &, const Foo<T&);

template <typename T>
class Foo {
private:
T value;
public:
Foo(const T & v) : value(v) {}
friend std::ostream & operator << <(std::ostrea m &, const Foo<T&);
};

// implement operator <<
template <typename T>
std::ostream & operator << (std::ostream &o, const Foo<T&f) {
return o << f.value ;
}

int main() {
Foo<intfi;
std::cout << fi;

return 0;
}

/* Here are the compiler error messages
Test.cpp: In function int main():
Test.cpp:26: error: no matching function for call to Foo<int>::Foo()
Test.cpp:16: note: candidates are: Foo<T>::Foo(con st T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(c onst Foo<int>&)
*/

*************** *************** **************

Dec 6 '07 #2
Once you not provide a constructor ,There will be a default
constructor;
When you provide, There will be your provided constructor only;
Dec 7 '07 #3

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

Similar topics

3
2251
by: MoCha | last post by:
hello everyone, i want to know of any trick used to restrict the access scope of a friend class (or function). ie instead of the friend class having access to the entire internals of the class in question, let it have access to a few private/protected members. this problem is cropping up several times. giving the friend class full access does not seem right n at the same time the function(s) r not reqd by anyone else.
4
2231
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the amount my operator has for bother cin , cout operator<< and >> thanks Dan
0
1208
by: Andrea Cacciarru | last post by:
Why template <class T> class TemplateFriend; //Forward Class "Template" Declaration class A { friend class TemplateFriend; //Any type T! }; compile well with VC++ 7 but do not compile wit gcc or Comeau? What says Standard about friend template class declaration?
1
1275
by: PengYu.UT | last post by:
Hi, The following program(doesn't compile) shows that I want to define class A with different template arguments "int" and "double". Because class A with int or with double are very different, I have define then separately. But it doesn't work. Could you show me the right way? Best wishes,
4
1519
by: PengYu.UT | last post by:
Hi, I try to define a friend template function in the following example. But I failed. Would you please help me what is wrong with the following code? Best wishes, Peng #include <iostream>
1
1429
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i created. But, I can't get the friend class to work. For example (this is not actually my code): template <typename Key, typename Value> class MyMap; template <typename Key, typename Value> struct MyMapNode;
0
1327
by: Adrian | last post by:
Hi All, I am trying to create an output operator that doesnt rely on the stream type and is also polymorphic. Is there a way to combine these 2 methods so that the polymorphic output operator has template arguments. I have tried a few ways and my compiler just wont let me. Method 1: Doesn't rely on the char type of a stream --------------------------------------------------- class A
7
1606
by: Nike | last post by:
The following code doesn't compile. I have a requirement in my project to declare one template as a friend function to another.. Can anyone suggest a way???? #include <iostream.h> template <class B,class Cclass A; //forward declaration template <class DBT_> class D{ public: friend class A<B,C>; //Error as B and C are not known as template
3
1739
by: tonvandenheuvel | last post by:
Hi all, the following code does not compile in gcc 4.0.2: 3 class Outer 4 { 5 class B; 6 7 template<typename T>
0
9712
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10089
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7634
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.