GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Feb-2005, 11:08
eggie eggie is offline
New Member
 
Join Date: Feb 2005
Posts: 3
eggie is on a distinguished road

help on how to reload ostream operator in a template class


Hello everyone,
I am trying to learn C++ by myself. Well, I might be too old to learn a computer language. I need a hand to write a matrix library for a simulation.
My question is how to reload the ostream operator << in a template the class.
I tried the following way, but it didnt work:
1.
CPP / C++ / C Code:
template <class T> class vec {
private:
T* p; int col;
public:
bla bla bal //constructor and destructor
template<class T> friend ostream& operator << (ostream& os, vec<T>& a);
};

template<class T> ostream& operator << (ostream& os, vec<T>& a) {
for (int i= 0; i<a.col ; i++)
os << a.p[i] << "  ";
return os;
}
Last edited by LuciWiz : 08-Feb-2005 at 13:20.
  #2  
Old 07-Feb-2005, 11:40
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
I'd be happy to help, if you could also provide the error message that your compiler issues in response to this code. If there are any other portions of code that might be relevant, such as #include statements, please include those lines as well.

One more note: this forum supports special tags for posting C/C++ code samples, which highlight the languages' syntactical constructs and produce a more readable example.

Like this:
CPP / C++ / C Code:
template <class T> class vec {
private:
  T* p; int col;
public:
  //bla bla bal //constructor and destructor
  template<class T> friend ostream& operator << (ostream& os, vec<T>& a);
};

template<class T> ostream& operator << (ostream& os, vec<T>& a) {
  for (int i= 0; i<a.col ; i++)
    os << a.p[i] << " ";
  return os;
}

To start the code sample, use the tag [c++ ], without the extra space I inserted before the closing brace. To end it, use [/c++ ].

Matthew
  #3  
Old 07-Feb-2005, 11:51
eggie eggie is offline
New Member
 
Join Date: Feb 2005
Posts: 3
eggie is on a distinguished road
Thanks a lot, Matthew.
Here is the code:
CPP / C++ / C Code:
# include <iostream>
using namespace std;

template<class T>
class vec {
 private:
  int col;
  T *p;
 public:
  //constructor
  vec();

 //operator reloaded
  template<class T>  friend  ostream& operator<< (ostream& os, const vec<T>& a);
 };
template <class T>  ostream& operator<< (ostream& os, const vec<T>& a){
  for (int i=0 ; i<a.col ; i++)
    {
      //      double x= a.p[i];
      os << a.p[i]  << "  ";
    }
  return os;
  }

error message:
vec.h:20: error: declaration of `class T'
vec.h:6: error: shadows template parm `class T'


thanks,
  #4  
Old 07-Feb-2005, 13:32
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
eggie,

The error message indicates that there is a conflict between the friend declaration
CPP / C++ / C Code:
  template<class T>  friend  ostream& operator<< (ostream& os, const vec<T>& a);
and the earlier line
CPP / C++ / C Code:
template<class T>
class vec {

Some compilers will not complain about your code, but according to the specification for Standard C++, you may not redeclare a template parameter. By typing "template<class T>" again inside the class declaration, you are redeclaring the symbol "T". I don't know if this explanation makes much sense, but one solution is to change your friend declaration to the following:
CPP / C++ / C Code:
  friend  ostream& operator<< <T> (ostream& os, const vec<T>& a);
This is called a "bound friend declaration," because you are using the same template parameter for the friend function that was used for the class. It is possible to declare unbound friends that have their own independent template parameters, but in your case that would not make sense.

Feel free to ask further questions.

Matthew

P.S. Most often, you will see the term "overloading" used to refer to the type of polymorphism in your code.
  #5  
Old 07-Feb-2005, 17:14
eggie eggie is offline
New Member
 
Join Date: Feb 2005
Posts: 3
eggie is on a distinguished road
Thank you so much, my friend.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
non-member function question crq CPP / C++ Forum 1 03-Feb-2005 21:59
What is "Ambigious symbol" ??*( a compilation error) small_ticket CPP / C++ Forum 2 07-Jan-2005 21:10
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 CPP / C++ Forum 2 19-Dec-2004 06:06
hashing help saiz66 CPP / C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.