Skip to main content
gamedev.net
🔒 Locked

Templates

Started by Agincourt Dec 6, 2001 at 6:37 PM 9 replies 750+ views
Original Post
Agincourt
Agincourt
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
Use the WriteCoolGame() functionWorks every time
Shannon Barber
Shannon Barber
try something like this:
  
class Queue
{
friend template <class T>
ostream& operator<<(ostream& ostr, const Queue<T>& Q);
};

template <class T>
ostream& operator<<(ostream& ostr, const Queue<T>& q)
{
//print each element

}

The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Agincourt
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
Use the WriteCoolGame() functionWorks every time
Oluseyi
Oluseyi
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).
Agincourt
Agincourt
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.
Use the WriteCoolGame() functionWorks every time
dankydoo
dankydoo
Hmm...try this in place of the line:

  friend ostream & operator<< <T> (ostream &ostr, const Queue< T> &Q);[  


dankjydoo

i took out my template......sorry...

Edited by - dankydoo on December 6, 2001 10:19:18 PM
Agincourt
Agincourt
How is that different from what I have? besides the fact that it takes away the <> which the compiler tells me it needs
Use the WriteCoolGame() functionWorks every time
Cyberdrek
Cyberdrek
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
[Cyberdrek | ]
Agincourt
Agincourt
Ok, so it''s ansi compliant, i''d still like a mouse! maybe it''s not the compiler I have such a problem with, it''s the damn editor. PICO is a piece of crap.
Use the WriteCoolGame() functionWorks every time
dankydoo
dankydoo
I edited my last post..try that....
dankydoo
dankydoo
I edited my last post..try that....

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.