operator<< in a template class

E

Eric Lilja

Ok, this code doesn't compile:
#include <iostream>
#include <ostream> /* Just for you, Mike :) */

template<typename T>
class Couple
{
public:
Couple(const T& ax, const T& ay) : x(ax), y(ay) {}

template<typename>
friend std::eek:stream& operator<<(std::eek:stream&, const Couple<T>&);
private:
T x;
T y;
};

template<typename T>
std::eek:stream& operator<<(std::eek:stream & o, const Couple<T>& p ) {
o << "(" << p.x << "," << p.y << ")" << std::flush;
return o;
}

int main() {
Couple<double> p1(1,2);
Couple<double> p2(1.123,4.53);
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
}

g++ -Wall -W -ansi -pedantic -g -c -o issue.o issue.cpp
issue.cpp: In function 'std::eek:stream& operator<<(std::eek:stream&, const
Couple<T>&) [with T = double]':
issue.cpp:25: instantiated from here
issue.cpp:13: error: 'double Couple<double>::y' is private
issue.cpp:18: error: within this context
issue.cpp:12: error: 'double Couple<double>::x' is private
issue.cpp:18: error: within this context

How do I make it work without having a standalone output function and public
data members in the class?

Thanks!

/ Eric
 
E

Etienne Rousee

Eric Lilja said:
Ok, this code doesn't compile:
In the declaration of the class Couple, replace:
template<typename>
friend std::eek:stream& operator<<(std::eek:stream&, const Couple<T>&);
by only:
friend std::eek:stream& operator<<(std::eek:stream&, const Couple<T>&);


Etienne
 
E

Eric Lilja

Etienne Rousee said:
In the declaration of the class Couple, replace:
template<typename>
friend std::eek:stream& operator<<(std::eek:stream&, const Couple<T>&);
by only:
friend std::eek:stream& operator<<(std::eek:stream&, const Couple<T>&);

Thanks but I already tried that and then I get the following warning:
issue.cpp:10: warning: friend declaration 'std::eek:stream&
operator<<(std::eek:stream&, const Couple<T>&)' declares a non-template
function
issue.cpp:10: warning: (if this is not what you intended, make sure the
function template has already been declared and add <> after the function
name here) -Wno-non-template-friend disables this warning
And then the application refuses to link... =/ Any other ideas?

/ Eric
 
E

Etienne Rousee

Eric Lilja said:
Thanks but I already tried that and then I get the following warning:
issue.cpp:10: warning: friend declaration 'std::eek:stream&
operator<<(std::eek:stream&, const Couple<T>&)' declares a non-template
function
issue.cpp:10: warning: (if this is not what you intended, make sure the
function template has already been declared and add <> after the function
name here) -Wno-non-template-friend disables this warning
And then the application refuses to link... =/ Any other ideas?

With Windows 2000 and VC++6.0, I have no warning and the link is ok.
I don't put "template<typename>" in the declaration of the class Couple,
put it is obviously necessary after in the declaration of the operator<<.

Etienne
 
E

Eric Lilja

Etienne Rousee said:
With Windows 2000 and VC++6.0, I have no warning and the link is ok.
I don't put "template<typename>" in the declaration of the class Couple,
put it is obviously necessary after in the declaration of the operator<<.

Well, using a more recent compiler than you (GCC 4.0.0), I can't get it to
work. Thanks anyway.

/ Eric
 
F

Fraser Ross

Ok, this code doesn't compile:
#include <iostream>
#include <ostream> /* Just for you, Mike :) */
template<typename T>
class Couple;

template <typename T>
std::ostream& operator said:
template<typename T>
class Couple
{
public:
Couple(const T& ax, const T& ay) : x(ax), y(ay) {}

template<typename>
friend std::eek:stream& operator<<(std::eek:stream&, const Couple<T>&);
friend std::eek:stream& operator<< <T>(std::eek:stream &, Couple const&);

The rest of you code is ok. The friend declaration cannot define a template
instance because of a rule.

Fraser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top