473,386 Members | 1,726 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,386 software developers and data experts.

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::ostream &, 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(const T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(const Foo<int>&)
*/

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

"Joe Hesse" <jo*******@actcx.comwrote in message
news:13*************@corp.supernews.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::ostream &, 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(const T&) [with T = int]
Test.cpp:12: note: Foo<int>::Foo(const 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
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...
4
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...
0
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...
1
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...
4
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
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...
0
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...
7
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...
3
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.