Problem with function template specialization
Patrick Percot
ppercot@free.fr
Thu Dec 30 22:15:00 GMT 2004
On Thu, 30 Dec 2004 11:47:19 -0500, Christian Convey <conveycj@npt.nuwc.navy.mil> wrote
> Hello,
Hi,
>
> I'm trying to write a small library of function templates, all named
> "toString", that will produce a string rendition of various data types.
>
> My problem is that when I try to create a specialized version of the
> toString() function for a particular data type, I get an error:
>
Don't you think that operator overloading is a better and more elegant
solution ?
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
class Foo {
private:
int x;
int y;
public:
Foo(int xv, int yv)
: x(xv)
, y(yv) {};
friend ostream& operator<< (ostream& os, const Foo& f);
};
ostream& operator<< (ostream& os, const Foo& f) {
os << "(" << &f << ") = Foo(" << f.x << ", " << f.y << ")";
}
template <typename T>
string toString(const T& val) {
ostringstream os;
os << val;
return os.str();
}
int main() {
Foo f(1,2);
cout << toString(f) << endl;
}
[..Zappé 59 lignes et 1233 caractères..]
À+
PP
--
NE MANGEZ PAS DE FOIE GRAS : http://www.stopgavage.com
Groupe Morbihannais d'Utilisateurs de Logiciels Libres http://www.tuxbihan.org
GPG fingerprint = 1A4F E154 3D2C A20E E4CA A543 7951 C5C2 E44A A0B5
Patrick Percot.
More information about the Gcc-help
mailing list