Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Define friend operator << for class template.

Reply
Thread Tools

Define friend operator << for class template.

 
 
Joe Hesse
Guest
Posts: n/a
 
      12-06-2007
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:stream & operator << (std:stream &, const Foo<T> &);

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

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

int main() {
Foo<int> fi;
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>&)
*/

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


 
Reply With Quote
 
 
 
 
Joe Hesse
Guest
Posts: n/a
 
      12-06-2007
Please forgive me, I have an obvious error. No response is needed from the
newsgroup.

"Joe Hesse" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:stream & operator << (std:stream &, const Foo<T> &);
>
> template <typename T>
> class Foo {
> private:
> T value;
> public:
> Foo(const T & v) : value(v) {}
> friend std:stream & operator << <> (std:stream &, const Foo<T> &);
> };
>
> // implement operator <<
> template <typename T>
> std:stream & operator << (std:stream &o, const Foo<T> &f) {
> return o << f.value ;
> }
>
> int main() {
> Foo<int> fi;
> 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>&)
> */
>
> ********************************************
>



 
Reply With Quote
 
 
 
 
terminator
Guest
Posts: n/a
 
      12-06-2007
On Dec 6, 8:05 pm, "Joe Hesse" <(E-Mail Removed)> wrote:
> 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:stream & operator << (std:stream &, const Foo<T> &);
>
> template <typename T>
> class Foo {
> private:
> T value;
> public:
> Foo(const T & v) : value(v) {}
> friend std:stream & operator << <> (std:stream &, const Foo<T> &);
>
> };
>
> // implement operator <<
> template <typename T>
> std:stream & operator << (std:stream &o, const Foo<T> &f) {
> return o << f.value ;
>
> }
>
> int main() {
> Foo<int> fi;
> 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>&)
> */
>
> ********************************************


Instein says "take it simple, as simple as posible but not simpler".
Why did you not use the simplest imaginable syntax?
I would write:

friend std:stream & operator << (std:stream &, const Foo &);

but this is not what the compiler complaigns about;Please learn to
read:

> 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>&)


Since you have defined a constructor ,C++ will no more automatically
generate a default constructor. this is the errorneous line:

Foo<int> fi;

In order to resolve this add the following inside the braces for
declaration of your template class:

Foo(){};

regards,
FM.

 
Reply With Quote
 
want.to.be.professer
Guest
Posts: n/a
 
      12-07-2007
Once you not provide a constructor ,There will be a default
constructor;
When you provide, There will be your provided constructor only;
 
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
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
Overloading operator<< as a friend to a template class CrimzonRJ@gmail.com C++ 4 09-30-2006 01:27 PM
making ostream& operator<< a friend function of a class in a namespace Tim Partridge C++ 1 06-02-2004 05:07 PM
friend operator>> function declaration for a template class Yu Lianqing C++ 1 04-04-2004 01:36 AM
if i do a friend Testclass operator +(Test class &obj2); it is not correct because i need two objects? John Cho C++ 1 03-03-2004 07:48 AM



Advertisments