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

Problem with declaring a template friend.

Hello, all.

I saw this in the provided code of a lab that I have due in a couple of
weeks in my algorithms class. It compiled fine in g++ but did not compile
with the VC++ compiler. It does not recognize the angle brackets that
follow "<<" in the operator friend declaration.

--- CODE ---

template <class T> class PriorityQueue {
....
friend std::ostream &operator<< <> (std::ostream &, PriorityQueue &);
}

template <class T>
std::ostream &operator<<(std::ostream &os, PriorityQueue<T> &q)
{
os << "not implemented yet\n";
return os;
}

--- END CODE ---

Is this code standard? If so, how do you suggest I get around the weakness
in the VC++ compiler? If not, how should I write this?

Sincerely,

James
Jul 22 '05 #1
5 1435
A quick note: requesting help on syntax and language semantics is not
forbidden in my class, so worry not about whether or not it would be
cheating to help me.
Jul 22 '05 #2
James Aguilar wrote in news:co**********@newsreader.wustl.edu in
comp.lang.c++:
Hello, all.

I saw this in the provided code of a lab that I have due in a couple
of weeks in my algorithms class. It compiled fine in g++ but did not
compile with the VC++ compiler. It does not recognize the angle
brackets that follow "<<" in the operator friend declaration.

Add the following declarations before you class:

template <class T> class PriorityQueue; /* needed next ... */

template <class T>
std::ostream &operator<<(std::ostream &os, PriorityQueue<T> &q);

--- CODE ---

template <class T> class PriorityQueue {
...
friend std::ostream &operator<< <> (std::ostream &, PriorityQueue &);
}

template <class T>
std::ostream &operator<<(std::ostream &os, PriorityQueue<T> &q)
{
os << "not implemented yet\n";
return os;
}

--- END CODE ---

Is this code standard?
No, add the declaration before the class, the compiler needs to
know that operator << is template before the friend declaration.
If so, how do you suggest I get around the
weakness in the VC++ compiler? If not, how should I write this?


The VC compiler is Standard conforming in this regard and latter
versions of g++ (3.4 and 4.0) are too.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
The VC compiler is Standard conforming in this regard and latter
versions of g++ (3.4 and 4.0) are too.


So if I had g++ 3.3.3 for Cygwin, that might have been why it would compile
without those two declarations? Excellent, and thank you for the help. It
works now on the MS compiler.

I also found that if I do

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

instead of

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

it still works. Is there a real difference? Why do I need those empty
brackets in the first place? (I understand the general idea and most of the
semantics of templates, but some of the less commonly encountered points
still escape me.)

Once again, thanks for the help!

James
Jul 22 '05 #4
James Aguilar wrote:
"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
The VC compiler is Standard conforming in this regard and latter
versions of g++ (3.4 and 4.0) are too.

So if I had g++ 3.3.3 for Cygwin, that might have been why it would compile
without those two declarations? Excellent, and thank you for the help. It
works now on the MS compiler.

I also found that if I do

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

instead of

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

it still works. Is there a real difference? Why do I need those empty
brackets in the first place? (I understand the general idea and most of the
semantics of templates, but some of the less commonly encountered points
still escape me.)

Once again, thanks for the help!

James

I think it is parsing problem. A reason why C++ and templates are
syntactic nightmares!!!
The compiler is confused whether you meant < << > or << <> .

Hang on there must be few C++ experts around who will clarify it.

Even I was confused when I first saw the code wondering whether you wanted.
--
Surendra Singhi

www.public.asu.edu/~sksinghi
Jul 22 '05 #5
James Aguilar wrote in news:co**********@newsreader.wustl.edu in
comp.lang.c++:
"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
The VC compiler is Standard conforming in this regard and latter
versions of g++ (3.4 and 4.0) are too.
So if I had g++ 3.3.3 for Cygwin, that might have been why it would
compile without those two declarations? Excellent, and thank you for
the help. It works now on the MS compiler.

I also found that if I do

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


This had me confused for a while, I thought you meant that, the above
works without the forward declarations, but it doesn't (MSVC 7.1).
instead of

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

it still works. Is there a real difference?
Not really, the first (<T>) form is just more explicit than the second.

In the second form the compiler deduces the T for you from the forward
declaration and the paramiter types you give in the friend declaration.

Why do I need those
empty brackets in the first place? (I understand the general idea and
most of the semantics of templates, but some of the less commonly
encountered points still escape me.)


The empty brackets tell the compiler that you are befriending a
template function, without them, it will make your class a friend of
the non-template:

std::ostream &operator << ( std::ostream &, PriorityQueue< T > & );

This declaration *doesn't* refer to a specialization of the template
function:

template < typename T >
std::ostream &operator << ( std::ostream &, PriorityQueue< T > & );

You would have to provide an (overloaded) defenition for each type:

std::ostream &operator << ( std::ostream &, PriorityQueue< int > & )
{
return os << "int overload";
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

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

Similar topics

2
by: keit6736 | last post by:
Hi, I'm using the Borland compiler and I've created two templated classes in which I've overloaded the ostream << operator. However, when I try and use the operator on objects of either class I...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
2
by: PengYu.UT | last post by:
The following program works with g++3.3. I'm assuming g++-3.4 is more standard conforming that g++-3.3. Would you please tell me what is right way to declare a template friend function of a...
8
by: flamexx7 | last post by:
Can anybody tell me what is wrong with declaration of pointer p ? template<class Tclass Stack { struct Link { T* data; Link* next; Link(T* dat, Link* nxt) : data(dat), next(nxt) {} }* head;...
3
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on WebForm2.aspx. For declaring the property oRpte()...
2
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
1
by: curious2007 | last post by:
Hello everybody, When I try to declare nontemplate friend functions I get the following warning: friend decleration......declares a non-template function (if this is not what you intended,...
4
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.