473,413 Members | 1,802 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,413 software developers and data experts.

operator overloading in templates

Hi,

I added an operator overloader in my template
class:

graph.h:

template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}

graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}
While compiling with gcc I get the warning:
In file included from graph.cpp:2:
.../graph.h:80: warning: friend declaration `std::ostream&
operator<<(std::ostream&, Node<NODE>&)' declares a
non-template function
.../graph.h:80: 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

What is the reason for that warning and how can I avoid it?

Thank you.

Regards,
Chris
Jul 23 '05 #1
10 1870
Christian Christmann wrote:
I added an operator overloader in my template
class:

graph.h:
Add here:

template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);

And, is there any particular reason why your operator<< needs it second
argument as a reference to a *non-const* Node<>?

template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}

graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}
While compiling with gcc I get the warning:
In file included from graph.cpp:2:
../graph.h:80: warning: friend declaration `std::ostream&
operator<<(std::ostream&, Node<NODE>&)' declares a
non-template function
../graph.h:80: 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

What is the reason for that warning and how can I avoid it?


Declare your operator<< as a template before the class template. See
above.

V
Jul 23 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:s%*******************@newsread1.mlpsca01.us.t o.verio.net...
Christian Christmann wrote:
I added an operator overloader in my template
class:

graph.h:


Add here:

template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);


Why are these declarations necessary? I notice that neither VS 2003 nor VC++
6.0 needs them.
template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}

graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}


DW
Jul 23 '05 #3
David White wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:s%*******************@newsread1.mlpsca01.us.t o.verio.net...
Christian Christmann wrote:
I added an operator overloader in my template
class:

graph.h:
Add here:

template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);

Why are these declarations necessary? I notice that neither VS 2003 nor VC++
6.0 needs them.


The Standard, in 14.5.3/1, says that if the friend function declaration
that is not a template declaration (does not begin with the 'template'
keyword) and the name of the function is a template-id, the friend
declaration refers to a template specialisation, if it's not a template-id
but an 'unqualified-id', it declares an ordinary function. So, if you (or
the OP) don't declare operator<< as a template beforehand, it is
considered a simple function, which goes against the fact that its second
argument depends on 'NODE'.

The conflict is resolved if 'operator<<' is a template-id, which is
achieved by declaring 'operator<<' a template (that's the second template
declaration above 'Node'). The declaration of 'Node' as a class template
(the very first declaration statement) is necessary because it's used in
the following function template declaration.

template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}

graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}

DW


V
Jul 23 '05 #4
Add here:

template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);

After adding the two lines before the class declaration and compiling I
still get the same error message.

And, is there any particular reason why your operator<< needs it second
argument as a reference to a *non-const* Node<>?
What would you suggest instead?

Thank you.
V


Chris

Jul 23 '05 #5
Christian Christmann wrote:
Add here:

template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);
After adding the two lines before the class declaration and compiling I
still get the same error message.


FAQ 5.8.

And, is there any particular reason why your operator<< needs it second
argument as a reference to a *non-const* Node<>?

What would you suggest instead?


A reference to a *const* Node<>. Or does your output operator actually
change the Node?

V
Jul 23 '05 #6
On Wed, 18 May 2005 18:28:56 -0400, Victor Bazarov wrote:
Christian Christmann wrote:
Add here:

template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);
After adding the two lines before the class declaration and compiling I
still get the same error message.


FAQ 5.8.

Sorry, but I don't really know what I did wrong. The message I get is the
same as in my first post:

graph.h:83: warning: friend declaration `std::ostream&
operator<<(std::ostream&, Node<NODE>&)' declares a
non-template function
graph.h:83: 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
If you expect something else, please let me know.

And, is there any particular reason why your operator<< needs it second
argument as a reference to a *non-const* Node<>?

What would you suggest instead?


A reference to a *const* Node<>. Or does your output operator actually
change the Node?


No, Node is not going to be changed. So, you mean defining Node as const ?

friend ostream &operator<< (ostream &, const Node<NODE> &);


V

Chris
Jul 23 '05 #7
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:hD*******************@newsread1.mlpsca01.us.t o.verio.net...
David White wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:s%*******************@newsread1.mlpsca01.us.t o.verio.net...
Add here:

template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);

Why are these declarations necessary? I notice that neither VS 2003 nor VC++ 6.0 needs them.


The Standard, in 14.5.3/1, says that if the friend function declaration
that is not a template declaration (does not begin with the 'template'
keyword) and the name of the function is a template-id, the friend
declaration refers to a template specialisation, if it's not a template-id
but an 'unqualified-id', it declares an ordinary function. So, if you (or
the OP) don't declare operator<< as a template beforehand, it is
considered a simple function, which goes against the fact that its second
argument depends on 'NODE'.

The conflict is resolved if 'operator<<' is a template-id, which is
achieved by declaring 'operator<<' a template (that's the second template
declaration above 'Node'). The declaration of 'Node' as a class template
(the very first declaration statement) is necessary because it's used in
the following function template declaration.


It will take me a while to get my head around this. I thought the friend
declaration in the class definition looked unambiguously like a template for
the friend function (for the reason you pointed out; the second argument
contains the template parameter NODE), and that seems to be how my compilers
interpreted it. A member function in a class template might have a template
parameter among its arguments too. Anyway, thanks for the explanation.

DW
Jul 23 '05 #8
Christian Christmann wrote:
[..]> Sorry, but I don't really know what I did wrong. The message I get
is
the
same as in my first post:
[..]


I don't know what to tell you. Try using a later version of the compiler.

V
Jul 23 '05 #9
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<KN********************@comcast.com>...
Christian Christmann wrote:
[..]> Sorry, but I don't really know what I did wrong. The message I get
is
the
same as in my first post:
[..]


I don't know what to tell you. Try using a later version of the compiler.

V


/*
I posted something same few months ago and only solution that I can
find is something like this:

template <typename T> class K {
T p;
public:
friend ostream& operator<< <T>(ostream&, K<T>&);
};

template <typename T> ostream& operator<<(ostream& c, K<T> &k)
{
return cout << k.podatak;
}

Note1: this code will not work on all compilers (at least Borland C++
5.0 issue error on this), gcc 3.2 compile above code OK. If you use
Borland 5.0 you must implement operator in class body and remove <T>
after operator <<.

Note2: it works with "const K<T>&"

Best,
Zaharije Pasalic
*/
#include <iostream>

using namespace std;

template <typename T> class Klasa
{
T podatak;
public:
friend ostream& operator<< <T>(ostream&, const
Klasa<T>&);
};

template <typename T> ostream& operator<<(ostream& c, const Klasa<T>
&k)
{
return cout << k.podatak;
}

int main() {
Klasa<int> a;
cout << a;
return 0;
}
Jul 23 '05 #10
On Wed, 18 May 2005 22:14:50 -0400, Victor Bazarov wrote:
Christian Christmann wrote:
[..]> Sorry, but I don't really know what I did wrong. The message I get
is
the
same as in my first post:
[..]
I don't know what to tell you. Try using a later version of the compiler.


I could fix the problem. What I had to do was to add <> in the header
file. The declaration of the operator has to be:

friend ostream &operator<< <>(ostream &, const Node<NODE> &);

V


Jul 23 '05 #11

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

Similar topics

51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
5
by: richard.parker | last post by:
Hello, I need to overload operator new with affecting the system libraries. Has anyone done this? I've got 2 static libraries and application source code where the operator needs to be...
11
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.