home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12697 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.3 KB  |  79 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!nntp-server.caltech.edu!cithe503.cithep.caltech.edu!panetta
  3. From: panetta@cithe503.cithep.caltech.edu (James Panetta)
  4. Subject: problem with overloading * in a template
  5. Message-ID: <1992Aug21.011916.19713@cco.caltech.edu>
  6. Keywords: templates, operator overloading
  7. Sender: news@cco.caltech.edu
  8. Nntp-Posting-Host: cithe503.cithep.caltech.edu
  9. Reply-To: panetta@cithe501.cithep.caltech.edu (James Panetta)
  10. Organization: California Institute of Technology, Pasadena
  11. Date: Fri, 21 Aug 1992 01:19:16 GMT
  12. Lines: 65
  13.  
  14. I am trying to implement an abstract vector datatype (how original!).
  15. The problem is, I need the length to be placed in the declaration.
  16. With this in mind, I am trying to overload the binary * operator to
  17. output the (unit metric) scalar product, and the syntax of the overload is
  18. giving me major problems.
  19.  
  20. Here is  my code:
  21.  
  22. #include <iostream.h>
  23.  
  24. template<class T,int n>  class vector
  25. {
  26.    protected:
  27.       T* data;
  28.       int siz;
  29.    public:
  30.       vector() : siz(n) , data(new T[n]) { /*verify*/ }
  31. /* standard dot product */
  32.       T operator *(const vector& , const vector& );
  33. };  /* class vector */
  34.  
  35. template <class T, int n>
  36. T vector<T,n>::operator *(const vector& v1, const vector& v2)
  37. {
  38.     T dotprod=0;
  39.        if ( (n != v1.siz)
  40.         || ( n != v2.siz)
  41.         || ( v1.siz != v2.siz ))
  42.    {
  43.       cout << "Vector size mismatch in dot product";
  44.       return 0;
  45.    }
  46.    for (int i=0 ; i < n ; i++) dotprod += v1.data[i] * v2.data[i];
  47.    return dotprod;
  48. }
  49.  
  50. main()
  51. {
  52.         vector<float,3> foo, bazz;
  53.         vector<float,5> bar;
  54.         float dp;
  55.  
  56. /* assume vectors have been set to something meaningful */
  57.  
  58.         dp = foo * bazz;
  59.         cout << "foo * bazz = " << dp << endl;
  60. }
  61.  
  62.  
  63. When I compile this code on a VAX using GNU C++ version 2.2.2 (vax vms)
  64. I receive the error message:
  65. vector.cc:type conversion required for binary operation on types 
  66.     `vector<float ,3>' and `vector<float ,3>'
  67.  
  68. I assume there is some problem in my defining sequences for the operator.
  69. I have checked in ARM and Barkakati's OOP in C++, and they aren't much help.
  70.  
  71. Please e-mail me with solutions.
  72.  
  73. Jim Panetta
  74.  
  75. -- 
  76. Panetta@cithex.caltech.edu          :-(     A witty saying proves nothing.
  77. Panetta@cithex.bitnet               :-|
  78. Panetta@cithe503.cithep.caltech.edu :-)                             Voltaire
  79.