PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Define friend operator << for class template.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Define friend operator << for class template.

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2007, 17h05   #1
Joe Hesse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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>&)
*/

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


  Réponse avec citation
Vieux 06/12/2007, 17h25   #2
Joe Hesse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Define friend operator << for class template.

Please forgive me, I have an obvious error. No response is needed from the
newsgroup.

"Joe Hesse" <joe_hesse@actcx.com> wrote in message
news:13lgau67k1b9c6c@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 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>&)
> */
>
> ********************************************
>



  Réponse avec citation
Vieux 06/12/2007, 17h26   #3
terminator
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Define friend operator << for class template.

On Dec 6, 8:05 pm, "Joe Hesse" <joe_he...@actcx.com> 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 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.

  Réponse avec citation
Vieux 07/12/2007, 09h01   #4
want.to.be.professer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Define friend operator << for class template.

Once you not provide a constructor ,There will be a default
constructor;
When you provide, There will be your provided constructor only;
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15501 seconds with 12 queries