J
Joe Hesse
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>&)
*/
********************************************
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>&)
*/
********************************************