help (overload << in a template class )

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

help (overload << in a template class )

 
0
  #1
Jun 16th, 2005
Hi, anyone can tell me what's wrong with my code? the only part has problem is the function overload << operator, any help is welcome.

#include <iostream>
using namespace std;

template <typename T, int SIZE>
class array
{
T data_[SIZE];
array (const array& other);
const array& operator = (const array& other);
public:
array(){};
T& operator[](int i) {return data_[i];}
const int getSize() const{return SIZE;};
const T& get_elem (int i) const {return data_[i];}
void set_elem(int i, const T& value) {data_[i] = value;}
operator T*() {return data_;}
friend ostream& operator<< <T, SIZE>(ostream&, const array<T, SIZE>&);
};

ostream& operator<< <T, SIZE> (ostream& out, const array<T, SIZE>& ar)
{
out<<"< ";
for(int i=0;i<ar.getSize();i++)
out<<ar.get_elem(i)<<" ";
out<<" >";
return out;
}

int main(void)
{
array<int, 10> intArray;
for(int i=0;i<10;i++) intArray.set_elem(i, i+100);
//std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl;
//int firstElem = intArray.get_elem(0);
//int* begin = intArray;
cout<<intArray;
}
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: help (overload << in a template class )

 
0
  #2
Jun 16th, 2005
operator<< doesn't need to be a friend since you only use the public interface of array in it. A non-member function only needs to be a friend if it has to have access to a classes private or protected members.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T, int SIZE>
  5. class array
  6. {
  7. T data_[SIZE];
  8. array (const array& other);
  9. const array& operator = (const array& other);
  10. public:
  11. array(){};
  12. T& operator[](int i) {return data_[i];}
  13. const int getSize() const{return SIZE;};
  14. const T& get_elem (int i) const {return data_[i];}
  15. void set_elem(int i, const T& value) {data_[i] = value;}
  16. operator T*() {return data_;}
  17. };
  18.  
  19. template <typename T, int SIZE>
  20. ostream& operator<<(ostream& out, const array<T, SIZE>& ar)
  21. {
  22. out<<"< ";
  23. for(int i=0;i<ar.getSize();i++)
  24. out<<ar.get_elem(i)<<" ";
  25. out<<" >";
  26. return out;
  27. }
  28.  
  29. int main(void)
  30. {
  31. array<int, 10> intArray;
  32. for(int i=0;i<10;i++) intArray.set_elem(i, i+100);
  33. //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl;
  34. //int firstElem = intArray.get_elem(0);
  35. //int* begin = intArray;
  36. cout<<intArray;
  37. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: help (overload << in a template class )

 
0
  #3
Jun 16th, 2005
Thanks a lot for the help! That solves my problem.
One more question (sorry i am messed by template), in case I don't have public interface and will access _data[] directly, then I will define << as a friend, how to write the code in that way? I always get non_template function warning when I format code as below.
/salute


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T, int SIZE>
  5. class array
  6. {
  7. T data_[SIZE];
  8. array (const array& other);
  9. const array& operator = (const array& other);
  10. public:
  11. array(){};
  12. T& operator[](int i) {return data_[i];}
  13. const int getSize() const{return SIZE;};
  14. const T& get_elem (int i) const {return data_[i];}
  15. void set_elem(int i, const T& value) {data_[i] = value;}
  16. operator T*() {return data_;}
  17. friend ostream& operator<<(ostream&, const array<T, SIZE>&);
  18. };
  19.  
  20. template <typename T, int SIZE>
  21. ostream& operator<<(ostream& out, const array<T, SIZE>& ar)
  22. {
  23. out<<"< ";
  24. for(int i=0;i<SIZE;i++)
  25. out<<ar.data_[i]<<" ";
  26. out<<" >";
  27. return out;
  28. }
  29.  
  30. int main(void)
  31. {
  32. array<int, 10> intArray;
  33. for(int i=0;i<10;i++) intArray.set_elem(i, i+100);
  34. //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl;
  35. //int firstElem = intArray.get_elem(0);
  36. //int* begin = intArray;
  37. cout<<intArray;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: help (overload << in a template class )

 
0
  #4
Jun 16th, 2005
write it this way and it works, but when I write function body of overload << out of class declaration
then I get error.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T, int SIZE>
  5. class array
  6. {
  7. T data_[SIZE];
  8. array (const array& other);
  9. const array& operator = (const array& other);
  10. public:
  11. array(){};
  12. T& operator[](int i) {return data_[i];}
  13. const int getSize() const{return SIZE;};
  14. const T& get_elem (int i) const {return data_[i];}
  15. void set_elem(int i, const T& value) {data_[i] = value;}
  16. operator T*() {return data_;}
  17. friend ostream& operator<<(ostream& out, const array<T, SIZE>& ar){
  18. out<<"< ";
  19. for(int i=0;i<SIZE;i++)
  20. out<<ar.data_[i]<<" ";
  21. out<<" >"<<endl;
  22. return out;
  23. }
  24. };
  25.  
  26. int main(void)
  27. {
  28. array<int, 10> intArray;
  29. for(int i=0;i<10;i++) intArray.set_elem(i, i+10);
  30. //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl;
  31. //int firstElem = intArray.get_elem(0);
  32. //int* begin = intArray;
  33. cout<<intArray;
  34. }
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: help (overload << in a template class )

 
0
  #5
Jun 16th, 2005
In that situation, you make the operator inline. The correct way to define it outside of your class is way too awkward to be practical:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T, int SIZE> class array;
  5. template <typename Ty, int sz>
  6. ostream& operator<<(ostream& out, const array<Ty, sz>& ar);
  7.  
  8. template <typename T, int SIZE>
  9. class array
  10. {
  11. T data_[SIZE];
  12. array (const array& other);
  13. const array& operator = (const array& other);
  14. public:
  15. array(){};
  16. T& operator[](int i) {return data_[i];}
  17. int getSize() {return SIZE;}
  18. const T& get_elem (int i) const {return data_[i];}
  19. void set_elem(int i, const T& value) {data_[i] = value;}
  20. operator T*() {return data_;}
  21.  
  22. friend ostream& operator<< <>(ostream& out, const array& ar);
  23. };
  24.  
  25. template <typename T, int SIZE>
  26. ostream& operator<<(ostream& out, const array<T, SIZE>& ar)
  27. {
  28. out<<"< ";
  29. for(int i=0;i<SIZE;i++)
  30. out<<ar.data_[i]<<" ";
  31. out<<" >";
  32. return out;
  33. }
  34.  
  35. int main(void)
  36. {
  37. array<int, 10> intArray;
  38. for(int i=0;i<10;i++) intArray.set_elem(i, i+100);
  39. //std::cout<<" element 0: "<<intArray.get_elem(0)<<std::endl;
  40. //int firstElem = intArray.get_elem(0);
  41. //int* begin = intArray;
  42. cout<<intArray;
  43. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: help (overload << in a template class )

 
0
  #6
Jun 16th, 2005
Indeed it's difficult for me to understand why, but it truely works. Guess in practice i will follow the
first two ways as they are much more clear.
Many thanks for the help!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 50
Reputation: farag is an unknown quantity at this point 
Solved Threads: 1
farag farag is offline Offline
Junior Poster in Training

Re: help (overload << in a template class )

 
0
  #7
Feb 22nd, 2008
can you berifly explain what compiler understand
when we declare template function befpre the class and declare it as friend function inside class and define it outside the class

Another Question
what this function mean
  1. operator T*() {return data_;}


thank you in advance
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: help (overload << in a template class )

 
0
  #8
Feb 23rd, 2008
what this function mean
  1. operator T*() {return data_;}
It returns a pointer to the member variable data_ of the array class.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 50
Reputation: farag is an unknown quantity at this point 
Solved Threads: 1
farag farag is offline Offline
Junior Poster in Training

Re: help (overload << in a template class )

 
0
  #9
Feb 23rd, 2008
what about

can you berifly explain what compiler understand
when we declare template function befpre the class and declare it as friend function inside class and define it outside the class


Another question

the function

  1. template <typename T, int SIZE>
  2. ostream& operator<<(ostream& out, const array<T, SIZE>& ar)
  3. {
  4. out<<"< ";
  5. for(int i=0;i<SIZE;i++)
  6. out<<ar.data_[i]<<" ";
  7. out<<" >";
  8. return out;
  9. }

gives an error if i replaced it by

  1. template <typename T, int SIZE>
  2. ostream& operator<<(ostream& out, const array<T, SIZE>& ar)
  3. {
  4. out<<"< ";
  5. for(int i=0;i<SIZE;i++)
  6. out<<ar.data_[i]<<" ";
  7. out<<" >";
  8. return out;
  9. }

althougth the [] operator is overloaded before
plz reply and explain
thanks alot
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC