#ifndef TINYVEC_HPP
#define TINYVEC_HPP
#include <iostream>
#include<iomanip>
using namespace std;


template<typename T_numtype, int N_length>
class TinyVector 
{
protected:

       T_numtype data[N_length];

public:


//////////////////////////////////////////////////////
    // Constructor 
//////////////////////////////////////////////

     TinyVector()  { }

     ~TinyVector() { }

     TinyVector(const TinyVector<T_numtype,N_length>& x);

     TinyVector(const T_numtype & initValue);

     TinyVector(const T_numtype  x[]);

      TinyVector(const T_numtype & x0, const T_numtype & x1);

    TinyVector(const T_numtype & x0, const T_numtype & x1, const T_numtype & x2);

     double  operator*(const TinyVector<T_numtype, N_length> &);
};

#include  "TinyVector.cxx"

#endif


template<typename T_numtype, int N_length>
double TinyVector<T_numtype, N_length>::operator*(const TinyVector<T_numtype, N_length> & x)
{
    double temp=0.0;
    for (int i=0; i < N_length; ++i)
        temp=temp+data[i]*x[i];
    return temp;
}

Dear freinds:
  I write a vector class template, but i can not compute a double number multiplies a vector, i have overload the * operator in the template, could you plese tell me how to resolve it.
     Regards

If you want to get the result of multiplying the entire contents of a TinyVector by a double and have the value returned as a double, (e.g. double result = myTinyVector * 5.034;) then this might work:
double operator *(const double &x);

Which you could define as:

template<typename T_numtype, int N_length>
double TinyVector<T_numtype, N_length>::operator*(const double &x)
{
    double temp=0.0;
    for (int i=0; i < N_length; ++i)
        temp+=data[i]*x;
    return temp;
}

Is that the kind of thing you're after?

line 49 needs to be:

temp = temp + (data[i] * x.data[i]);

If you are mulitplying each vector element agianst the other. One issue I see is how are you going to handle mismatched vector sizes?

There are a number of problems with your code.

First, if you are going to define the multiplication operator as a member function of the class template, you need to make it a const function:

// the prototype in the class template declaration:
double  operator*(const TinyVector<T_numtype, N_length> &) const;  //<-- notice 'const' here.

// the definition:
template <typename T_numtype, int N_length>
double TinyVector<T_numtype, N_length>::operator*(const TinyVector<T_numtype, N_length> & x) const  //<-- notice 'const' here.

This makes the object on which the member function is called const, just like the other operand (x) which is const too, which makes sense in a multiplication.

Second, why do you return a double? Why not the value-type of the vector itself:

T_numtype operator*(const TinyVector<T_numtype, N_length> &) const;

For example, if you created a TinyVector< std::complex<double>, 3>, then you would have trouble returning just a double from that. And if you had int, you would be doing an unnecessary conversion.

Third, it is usually discouraged to make operator overloading (and overloading in general) as member functions. This is because it can have some awkward quirks with regard to overload resolution (argument-dependent lookup), and hiding rules. It is also annoying when it comes to heterogeneous types, because you just can't use member functions only if you want to support both orderings of operands. So, unless a function is clearly only related to the one object on which the function is being called, prefer to use either a free function or a free friend function (if you really need access to private members, often you don't, especially with inline getters and setters).

So, you should generally prefer this free function:

template <typename T, int N>
T operator*(const TinyVector<T, N>& x, const TinyVector<T, N>& y)
{
    T temp = T(0);
    for (int i=0; i < N; ++i)
        temp += x[i] * y[i];           //<-- this requires an 'operator[]' in the class.
    return temp;
}

And, this way, you can also have a scalar multiplication function as:

template <typename T, int N>
TinyVector<T, N> operator*(const TinyVector<T, N>& x, const T& y)
{
    TinyVector<T, N> temp;
    for (int i=0; i < N; ++i)
        temp[i] = x[i] * y;
    return temp;
}

template <typename T, int N>
TinyVector<T, N> operator*(const T& x, const TinyVector<T, N>& y)
{
    return y * x;  //<-- use commutativity in this case (the call will be inlined, for sure).
}

At a more advanced level, it's often useful to allow for mixed value-types too. And C++11 (new standard) has a nice feature that helps for that, the decltype keyword and std::declval template. For example, you could define a mixed value-type operators as so:

template <typename T, typename U, int N>
decltype( std::declval<T>() * std::declval<U>() )
  operator*(const TinyVector<T, N>& x, const TinyVector<U, N> & y)
{
    typedef decltype( std::declval<T>() * std::declval<U>() ) result_type;
    result_type temp = result_type(0);
    for (int i=0; i < N; ++i)
        temp += x[i] * y[i];
    return temp;
}

template <typename T, typename U, int N>
TinyVector< decltype( std::declval<T>() * std::declval<U>() ), N> 
  operator*(const TinyVector<T, N>& x, const U& y) { /* ... */ }

template <typename T, typename U, int N>
auto operator*(const T& x, const TinyVector<U, N>& y) -> decltype( y * x ) {
    return y * x;
}

thank you very much dear friends; i resolved the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.