home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11205 < prev    next >
Encoding:
Text File  |  1992-07-20  |  2.5 KB  |  115 lines

  1. Path: sparky!uunet!darwin.sura.net!wupost!cs.utexas.edu!sun-barr!ames!agate!dog.ee.lbl.gov!network.ucsd.edu!deadmin.ucsd.edu
  2. From: rmr@deadmin.ucsd.edu (Robert Rother)
  3. Newsgroups: comp.lang.c++
  4. Subject: Help needed overloading "<<" with templates?
  5. Message-ID: <1912@deadmin.ucsd.edu>
  6. Date: 21 Jul 92 06:06:34 GMT
  7. Sender: rmr@deadmin.ucsd.edu
  8. Organization: UCSD Division of Engineering
  9. Lines: 104
  10.  
  11. Ok I give up.  I have more or less copied the template code for the
  12. Array class in Lippman's 2nd Edition C++ Primer (pg 380).  Using Turbo
  13. C++ 3.1 for windows I have two problems with the overloading of the
  14. "<<" operator.  First as noted before it won't compile without the
  15. addition of an "&" and second the call to the print member works while
  16. the use of the overloaded operator "<<" just prints a hex address.
  17. It's obvious to me that my overloaded operator is not being used by why
  18. is the mystery.
  19.  
  20. My sanity is more important than my ego at the moment so any help would
  21. be more than appreciated!
  22.  
  23. Robert Rother
  24. rmr@deadmin.ucsd.edu
  25.  
  26. P.S. I have no problem with the same code fragment minus the templates.
  27. -----------------------------------------------------------------------
  28. #include <assert.h>
  29. #include <iostream.h>
  30.  
  31. const int ArraySize = 12;
  32.  
  33. template <class Type> class Array;
  34.  
  35. template <class Type> ostream &
  36.     operator<<(ostream&, Array<Type>&);
  37.  
  38. template <class Type>
  39. class Array {
  40. public:
  41.     Array(int sz = ArraySize);
  42.     ~Array() { delete [] ia; }
  43.     void print(ostream& = cout);
  44. private:
  45.     void init(const Type *, Type);
  46.     int size;
  47.     Type *ia;
  48. };
  49.  
  50. template <class Type>
  51. Array<Type>::Array(int sz)
  52. {
  53.     size = sz;
  54.  
  55.      init(0, sz);
  56. }
  57.  
  58. template <class Type>
  59. void
  60. Array<Type>::init(const Type *array, int sz)
  61. {
  62.     ia = new Type[size = sz];
  63.     assert(ia != 0);
  64.  
  65.     for (int ndx = 0; ndx < size; ndx++)
  66.         ia[ndx] = (array != 0) ? array[ndx] : 0;
  67. }
  68.  
  69. template <class Type> ostream&
  70. operator << ( ostream& os, Array<Type>& ar)
  71. {
  72.     ar.print(os);
  73.     return(os);
  74. }
  75.  
  76. template <class Type>
  77. void Array<Type>::print(ostream& os)
  78. {
  79.     const int linelen = 12;
  80.  
  81.     os << "(" << size << ")<";
  82.     for (int ndx = 0; ndx < size; ndx++) {
  83.         if ((ndx % linelen) == 0 && ndx)
  84.             os << "\n\t";
  85.                 os << ia[ndx];
  86.         if ((ndx % linelen) != linelen - 1 && ndx != size - 1)
  87.             os << ", ";
  88.     }
  89.         os << " >\n";
  90. }
  91.  
  92. template <class Type>
  93. void try_it(Array<Type>& ar)
  94. {
  95.     cout << "Start\n";
  96.  
  97.     ar.print(cout);
  98.  
  99.         cout << "Middle\n";
  100.  
  101. // Note: the book shows the following but it wouldn't compile:
  102. // ***  cout << ar << endl;
  103.         cout << &ar << endl;
  104.  
  105.     cout << "Done!" << endl;
  106. }
  107.  
  108. main()
  109. {
  110.     Array<int> iA;
  111.  
  112.     try_it(iA);
  113.     return(0);
  114. }
  115.