🔒 Locked
Templates
Advertisement
Original Post
After mulling over this code for another 4-5 hours since my post last night, I've simplified the problem a little and was hoping with someone's help I can get this.
I have a templated class -
template < class T>
class Queue
{
public:
Queue< T>();
~Queue< T>();
friend ostream & operator<< <> (ostream &ostr, const Queue< T> &Q);
///More Functions...
};
the problem I'm having is that the operator << does not seem to get templated for the class, so when it's accessed I get a bunch of error messages like this -
assign9.C:122: no match for `ostream & << Queue< double> &'
each one is for a different type, and after the error message it lists all the things operator is overloaded for. A list so long in fact it hid the line above, making it difficult for me to pin down the problem. The < > after the operator << is requested by the compiler to tell it that it is a non-templated function that is a friend of a templated class. Here is the actual definition of the function -
template < class T>
ostream & operator << (ostream& ostr, const Queue< T> & node)
{
//Do some stuff
return ostr;
}
I've put well over 6-7 hours into this, looking it up and trying different things, so I'm really hoping somebody here knows the answer. PLEASE!! Thanks for any help.
Use the WriteCoolGame() function
Works every time
Edited by - Agincourt on December 6, 2001 8:05:36 PM
try something like this:
|
Unfortunately it didn''t work. I guess I forgot to mention in this post that I''m using the G++ compiler for unix. It''s for a class. I''m guessing your code works for a good compiler like MSVC++. Why in the hell we''re not using it baffles me (it came free with our book). God forbid we should be able to use a mouse or something like that. I''m dreading the class I have to take on the UNIX system. uggh.
Use the WriteCoolGame() function
Works every time
Use the WriteCoolGame() function
Works every time
quote:
Original post by Agincourt
friend ostream & operator<< <> (ostream &ostr, const Queue< T> &Q);
Why do you persist in inserting a spurious "<>"? It''s not part of the syntax is probably the reason why you keep getting this error. If your book or whatever source says it''s necessary, there''s a typo in the book. Cross-check it with other documentation (or the sources to the STL).
Because if I take it out I get this -
In file included from Queue.C:1:
Queue.h:34: warning: friend declaration `class ostream & operator <<(ostream &,
const Queue &)''
Queue.h:34: warning: declares a non-template function
Queue.h:34: warning: (if this is not what you intended, make sure
Queue.h:34: warning: the function template has already been declared,
Queue.h:34: warning: and add <> after the function name here)
Queue.h:34: warning: -Wno-non-template-friend disables this warning.
read "and add <> after the function name here" it''s G++ so maybe the convention is different from MSVC++?? I''ve tried just about every combination I can think of this to get it to work, I''ve also looked at G++ manuals and can''t find anything specific to this topic.
In file included from Queue.C:1:
Queue.h:34: warning: friend declaration `class ostream & operator <<(ostream &,
const Queue
Queue.h:34: warning: declares a non-template function
Queue.h:34: warning: (if this is not what you intended, make sure
Queue.h:34: warning: the function template has already been declared,
Queue.h:34: warning: and add <> after the function name here)
Queue.h:34: warning: -Wno-non-template-friend disables this warning.
read "and add <> after the function name here" it''s G++ so maybe the convention is different from MSVC++?? I''ve tried just about every combination I can think of this to get it to work, I''ve also looked at G++ manuals and can''t find anything specific to this topic.
quote:
Original post by Agincourt
Unfortunately it didn''t work. I guess I forgot to mention in this post that I''m using the G++ compiler for unix. It''s for a class. I''m guessing your code works for a good compiler like MSVC++. Why in the hell we''re not using it baffles me (it came free with our book). God forbid we should be able to use a mouse or something like that. I''m dreading the class I have to take on the UNIX system. uggh.
Use the WriteCoolGame() function
Works every time
Wow, back up a bit, G++ is way way better than VC++. And G++ is ansi C/C++ complient.
"And that''s the bottom line cause I said so!"
Cyberdrek
Headhunter Soft
A division of DLC Multimedia
Resist Windows XP''s Invasive Production Activation Technology!
"gitty up" -- Kramer
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement