John Harrison wrote:
I'm a bit confused with all these different operators and cases. Could you
provide a complete non-working code sample?
See below.
However one guess would be that you need
v3t0 * 5.0
Darn close! v3t0 * 5.0f
assuming the v3t0 is of type Vector3<double> because C++ will not deduce
the T is double from the first argument, only from the second, and since
you provided an int, it then looking for Vector3<int> as the first
argument.
That can't be exactly what's happening because it fails with both prefix and
postfix operator*(); I think the answer to my problem is to explicitly cast
the scaler operands to T. I.e., v3t0 * T(5);
The rules about when C++ can make deductions about template parameters are
quite complex and I'm not able to look them up right now so my explanation
above might not be completely accurate even if the suggested correction
is.
john
The code show results in this:
Fri Oct 01 13:20:19:> g++ -o vect main.cc
main.cc: In function `int main()':
main.cc:6: error: no match for 'operator*' in '5 * v0'
If I simply swap the comments show below, the code will compile. Note that T
is float in main();
/*
Vect.hh
*/
#ifndef VECT_HH
#define VECT_HH
#include <vector>
#include <iostream>
namespace{
using std::ostream;
using std::vector;
}
template<typename T>
class Vect{
public:
Vect(const T& x,
const T& y,
const T& z)
:_v(3)
{
_v[0]=x;
_v[1]=y;
_v[2]=z;
}
inline Vect<T>& operator*=(const T& n);
std::ostream& print(ostream& out) const;
private:
vector<T> _v;
};
template<typename T>
Vect<T>& Vect<T>::operator*=(const T& n)
{
_v[0] *= n;
_v[1] *= n;
_v[2] *= n;
return *this;
}
template<typename T>
//Vect<T> operator*(const double& n, const Vect<T>& v)
Vect<T> operator*(const T& n, const Vect<T>& v)
{
Vect<T> t(v);
return t *= n;
}
template<typename T>
//Vect<T> operator*(const Vect<T>& v, const double& n)
Vect<T> operator*(const Vect<T>& v, const T& n)
{
Vect<T> t(v);
return t *= n;
}
template<typename T>
ostream& Vect<T>::print(ostream& out) const
{
return out
<< "Vect<T> = {"
<< _v[0] << ","
<< _v[1] << ","
<< _v[2] << "}\n";
}
template<typename T>
ostream& operator<<(ostream& out, const Vect<T>& v)
{
return v.print(out);
}
#endif
/*
main.cc
*/
#include <iostream>
#include "Vect.hh"
int main() {
Vect<float> v0(3,4,5);
Vect<float> v1 = 5 * v0;
std::cout
<< "v0 == " << v0
<< "v1 == " << v1;
return 0;
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell