473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

operator<< in a template class

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

template<typename T>
std::ostream& operator<<(std::ostream & 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::ostream& operator<<(std::ostream&, 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
Jul 23 '05 #1
5 1859

"Eric Lilja" <mi****************************@gmail.com> a écrit ...
Ok, this code doesn't compile:

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


Jul 23 '05 #2

"Etienne Rousee" wrote:

"Eric Lilja" <mi****************************@gmail.com> a écrit ...
Ok, this code doesn't compile: In the declaration of the class Couple, replace:
template<typename>
friend std::ostream& operator<<(std::ostream&, const Couple<T>&);
by only:
friend std::ostream& operator<<(std::ostream&, const Couple<T>&);


Thanks but I already tried that and then I get the following warning:
issue.cpp:10: warning: friend declaration 'std::ostream&
operator<<(std::ostream&, 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?
Etienne


/ Eric
Jul 23 '05 #3

"Eric Lilja" <mi****************************@gmail.com> a écrit ...

"Etienne Rousee" wrote:

"Eric Lilja" <mi****************************@gmail.com> a écrit ...
Ok, this code doesn't compile:

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


Thanks but I already tried that and then I get the following warning:
issue.cpp:10: warning: friend declaration 'std::ostream&
operator<<(std::ostream&, 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
Jul 23 '05 #4

"Etienne Rousee" wrote:

"Eric Lilja" <mi****************************@gmail.com> a écrit ...

"Etienne Rousee" wrote:
>
> "Eric Lilja" <mi****************************@gmail.com> a écrit ...
>> Ok, this code doesn't compile:
> In the declaration of the class Couple, replace:
> template<typename>
> friend std::ostream& operator<<(std::ostream&, const Couple<T>&);
> by only:
> friend std::ostream& operator<<(std::ostream&, const Couple<T>&);
>
>
Thanks but I already tried that and then I get the following warning:
issue.cpp:10: warning: friend declaration 'std::ostream&
operator<<(std::ostream&, 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<<.


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

Etienne


/ Eric
Jul 23 '05 #5
> 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<< (std::ostream &, Couple<T> const&);
template<typename T>
class Couple
{
public:
Couple(const T& ax, const T& ay) : x(ax), y(ay) {}

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

friend std::ostream& operator<< <T>(std::ostream &, Couple const&);

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

Fraser.
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
1
by: Jacob Foshee | last post by:
Greetings, Using MS Visual C++ .NET 2003, I have the following: template <class T> class vector3 { // ... friend ostream& operator<<(ostream&, const vector3<T>& ); // ostream operator }:
4
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
8
by: Ook | last post by:
This is my code in it's entireity: #include <iostream> using namespace std; template <typename T> class SortedList { public: friend ostream& operator<< (ostream& os, const SortedList<T>&...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
0
by: Adrian | last post by:
Hi All, I am trying to create an output operator that doesnt rely on the stream type and is also polymorphic. Is there a way to combine these 2 methods so that the polymorphic output operator...
1
by: ruchirp | last post by:
Hi, I've been trying to overload << for a function similar to boost lambda library. I want to make for_each(v.begin(), b.end(), cout << _1 << endl); to work, however, i'm getting a lot of problems...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
2
by: Joe Hesse | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.