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:stream& operator<<(std:stream&, const Couple<T>&);
private:
T x;
T y;
};
template<typename T>
std:stream& operator<<(std: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:stream& operator<<(std: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
#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:stream& operator<<(std:stream&, const Couple<T>&);
private:
T x;
T y;
};
template<typename T>
std:stream& operator<<(std: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:stream& operator<<(std: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