home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!elroy.jpl.nasa.gov!nntp-server.caltech.edu!cithe503.cithep.caltech.edu!panetta
- From: panetta@cithe503.cithep.caltech.edu (James Panetta)
- Subject: problem with overloading * in a template
- Message-ID: <1992Aug21.011916.19713@cco.caltech.edu>
- Keywords: templates, operator overloading
- Sender: news@cco.caltech.edu
- Nntp-Posting-Host: cithe503.cithep.caltech.edu
- Reply-To: panetta@cithe501.cithep.caltech.edu (James Panetta)
- Organization: California Institute of Technology, Pasadena
- Date: Fri, 21 Aug 1992 01:19:16 GMT
- Lines: 65
-
- I am trying to implement an abstract vector datatype (how original!).
- The problem is, I need the length to be placed in the declaration.
- With this in mind, I am trying to overload the binary * operator to
- output the (unit metric) scalar product, and the syntax of the overload is
- giving me major problems.
-
- Here is my code:
-
- #include <iostream.h>
-
- template<class T,int n> class vector
- {
- protected:
- T* data;
- int siz;
- public:
- vector() : siz(n) , data(new T[n]) { /*verify*/ }
- /* standard dot product */
- T operator *(const vector& , const vector& );
- }; /* class vector */
-
- template <class T, int n>
- T vector<T,n>::operator *(const vector& v1, const vector& v2)
- {
- T dotprod=0;
- if ( (n != v1.siz)
- || ( n != v2.siz)
- || ( v1.siz != v2.siz ))
- {
- cout << "Vector size mismatch in dot product";
- return 0;
- }
- for (int i=0 ; i < n ; i++) dotprod += v1.data[i] * v2.data[i];
- return dotprod;
- }
-
- main()
- {
- vector<float,3> foo, bazz;
- vector<float,5> bar;
- float dp;
-
- /* assume vectors have been set to something meaningful */
-
- dp = foo * bazz;
- cout << "foo * bazz = " << dp << endl;
- }
-
-
- When I compile this code on a VAX using GNU C++ version 2.2.2 (vax vms)
- I receive the error message:
- vector.cc:type conversion required for binary operation on types
- `vector<float ,3>' and `vector<float ,3>'
-
- I assume there is some problem in my defining sequences for the operator.
- I have checked in ARM and Barkakati's OOP in C++, and they aren't much help.
-
- Please e-mail me with solutions.
-
- Jim Panetta
-
- --
- Panetta@cithex.caltech.edu :-( A witty saying proves nothing.
- Panetta@cithex.bitnet :-|
- Panetta@cithe503.cithep.caltech.edu :-) Voltaire
-