473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

overloading template operator arguments?

The comperable operator*() to that shown here works for a non-template
class:

template <typename T>
inline Vector3<T> operator*(const Vector3<T>& v, const T& n)
{
Vector3<T> t(v);
return t *= n;
}

For example let Vector3f be a vector of float values. This works fine:
inline Vector3f operator*(const Vector3f& v, const float& n)
{
Vector3f t(v);
return t *= n;
}

It also works when I overload the template form of the operator*=(). For
example this works for the same template class where the first function
shown above fails:

template <typename T>
inline Vector3<T>& Vector3<T>::operator*=(const T& n)
{
_v[0] *= n;
_v[1] *= n;
_v[2] *= n;
return *this;
}

If I replace the T& n with double& n in the first function, it works.

The error I get when using the first form above is this:
error: no match for 'operator*' in 'v3t0 * 5'

Where I have defined:
Vector3<T> v3t0;

Why?
--
"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

Jul 22 '05 #1
5 1681

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:38********************@speakeasy.net...
The comperable operator*() to that shown here works for a non-template
class:

template <typename T>
inline Vector3<T> operator*(const Vector3<T>& v, const T& n)
{
Vector3<T> t(v);
return t *= n;
}

For example let Vector3f be a vector of float values. This works fine:
inline Vector3f operator*(const Vector3f& v, const float& n)
{
Vector3f t(v);
return t *= n;
}

It also works when I overload the template form of the operator*=(). For
example this works for the same template class where the first function
shown above fails:

template <typename T>
inline Vector3<T>& Vector3<T>::operator*=(const T& n)
{
_v[0] *= n;
_v[1] *= n;
_v[2] *= n;
return *this;
}

If I replace the T& n with double& n in the first function, it works.

The error I get when using the first form above is this:
error: no match for 'operator*' in 'v3t0 * 5'

Where I have defined:
Vector3<T> v3t0;

Why?


I'm a bit confused with all these different operators and cases. Could you
provide a complete non-working code sample?

However one guess would be that you need

v3t0 * 5.0

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.

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
Jul 22 '05 #2
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

Jul 22 '05 #3
Steven T. Hatton wrote:
[...]
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;
}
Try

template<class T, class U> Vect<U> operator *(T t, const Vect<U>& v)
{
Vect<U> vv(v);
return vv *= t;
}
[..]


V
Jul 22 '05 #4

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:88********************@speakeasy.net...
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);


Yes I got that wrong. Template argument deduction is happening on both
parameters. But in the case of v3t0 * 5 you are giving contradictory
information, the first arg says T is float, the second says it's an int.

The best answer is as Victor says, use two template parameters so the
template argument deduction is independent on both parameters to operator*.

john
Jul 22 '05 #5
Victor Bazarov wrote:
Steven T. Hatton wrote:
[...]
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;
}


Try

template<class T, class U> Vect<U> operator *(T t, const Vect<U>& v)
{
Vect<U> vv(v);
return vv *= t;
}
[..]


V

Thanks! Works like a charm! :)
--
"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

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
10
by: Christian Christmann | last post by:
Hi, I added an operator overloader in my template class: graph.h: template <class NODE> class Node {
4
by: John Smith | last post by:
can we overload a javascript function with different argument? example: function a(a){} function a(a,b){}
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
7
by: AlanJSmith | last post by:
I am converting some C++ projects from 6 to vs2005 in one of the headers i have the code that follows this passage in which I get an error saying int default cant be assumed so i added...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
10
by: ozizus | last post by:
I overloaded operator << for STL map successfully: template <typename T1, typename T2ostream & operator << (ostream & o, map <T1,T2& m) { //code } the code works like a charm. Now, I want...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.