473,499 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to overload operator* for this template?

Hi,
I implemented a template as:
template <int L, int M, int T>
class Quantity
{
.....
public:
friend Quantity operator*(const Quantity& q1,const Quantity& q2);
.....
};

when I tried to use it as:
////////////////
typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;
const Acceleration GRAV(9.81);
const Length penLen(1);
....=penLen * GRAV;
////////////////////
The compiler complained "no operator defined for Acceleration".
If the both oprands have the same L,M,T, it works. But how to deal with
it if they are different?
And the return value is another issue, because it will have another set
of L,M,T.
I tried the following:
template <int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
..........
return q;
},

but failed with " error C2995: 'Quantity<L1*L2,M1*M2,T1*T2> operator
*(const Quantity<L1,M1,T1> &,const Quantity<L2,M2,T2> &)' : template
function has already been defined".
Thanks a lot.

Oct 14 '05 #1
13 1724
Atlas wrote:
Hi,
I implemented a template as:
template <int L, int M, int T>
class Quantity
{
....
public:
friend Quantity operator*(const Quantity& q1,const Quantity& q2);
....
};

when I tried to use it as:
////////////////
typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;
const Acceleration GRAV(9.81);
const Length penLen(1);
...=penLen * GRAV;
////////////////////
The compiler complained "no operator defined for Acceleration".
If the both oprands have the same L,M,T, it works. But how to deal with
it if they are different?
And the return value is another issue, because it will have another set
of L,M,T.
I tried the following:
template <int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
.........
return q;
},

but failed with " error C2995: 'Quantity<L1*L2,M1*M2,T1*T2> operator
*(const Quantity<L1,M1,T1> &,const Quantity<L2,M2,T2> &)' : template
function has already been defined".
Thanks a lot.


It's probably something to do with the order of your declarations. Post
a minimal but complete example that demonstrates the problem.

Cheers! --M

Oct 14 '05 #2
Atlas wrote:
Hi,
I implemented a template as:
template <int L, int M, int T>
class Quantity
{
....
public:
friend Quantity operator*(const Quantity& q1,const Quantity& q2);
....
};

when I tried to use it as:
////////////////
typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;
const Acceleration GRAV(9.81);
const Length penLen(1);
...=penLen * GRAV;
////////////////////
The compiler complained "no operator defined for Acceleration".
If the both oprands have the same L,M,T, it works. But how to deal with
it if they are different?
And the return value is another issue, because it will have another set
of L,M,T.
I tried the following:
template <int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
.........
return q;
},

but failed with " error C2995: 'Quantity<L1*L2,M1*M2,T1*T2> operator
*(const Quantity<L1,M1,T1> &,const Quantity<L2,M2,T2> &)' : template
function has already been defined".
Thanks a lot.


It may have something to do with the order of your declarations. Post a
minimal but complete piece of code that demonstrates the problem.

Cheers! --M

Oct 14 '05 #3
Atlas wrote:
I implemented a template as:
template <int L, int M, int T>
class Quantity
{
....
public:
friend Quantity operator*(const Quantity& q1,const Quantity& q2);
You probably want to drop this declaration from here. Does it really
need to be a friend? If so, you might want to make _all_ 'operator*'
templates friends of all the classes 'Quantity<>':

template<int L1, int M1, int T1, ...> // just like you did below
friend Quantity<L1+L2,M1+M2,T1+T2> operator*( ...
....
};

when I tried to use it as:
////////////////
typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;
const Acceleration GRAV(9.81);
const Length penLen(1);
...=penLen * GRAV;
////////////////////
The compiler complained "no operator defined for Acceleration".
If the both oprands have the same L,M,T, it works. But how to deal with
it if they are different?
And the return value is another issue, because it will have another set
of L,M,T.
Sure.
I tried the following:
template <int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
.........
return q;
},

but failed with " error C2995: 'Quantity<L1*L2,M1*M2,T1*T2> operator
*(const Quantity<L1,M1,T1> &,const Quantity<L2,M2,T2> &)' : template
function has already been defined".


Wrong compiler, maybe?
--------------------------------------- This:
template <int L, int M, int T>
class Quantity
{
public:
Quantity(double = 0);
template<int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2);
};

template<int L1, int M1, int T1, int L2, int M2, int T2>
Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1, const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
return q;
}

typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;

const Acceleration GRAV(9.81);
const Length penLen(1);

int main()
{
Quantity<2,0,-2> q = penLen * GRAV;
}
------------------------------------------- compiles fine with Comeau.

VC++ v8 chokes on it, but it's not something unexpected, really. MS'
compiler has been having troubles with templates and today isn't the
last day of it. I'll ask in microsoft.public.vc.language. Maybe they
already know of a bug reported on this...

V
Oct 14 '05 #4
Thanks so much for so many work. But I still can solve it. I tried your
codes in MSVC2003 but failed with "internal compiler error".

P.S. operator* has to be friend, to conform the internal type
convention.
Victor Bazarov wrote:
Atlas wrote:
I implemented a template as:
template <int L, int M, int T>
class Quantity
{
....
public:
friend Quantity operator*(const Quantity& q1,const Quantity& q2);


You probably want to drop this declaration from here. Does it really
need to be a friend? If so, you might want to make _all_ 'operator*'
templates friends of all the classes 'Quantity<>':

template<int L1, int M1, int T1, ...> // just like you did below
friend Quantity<L1+L2,M1+M2,T1+T2> operator*( ...
....
};

when I tried to use it as:
////////////////
typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;
const Acceleration GRAV(9.81);
const Length penLen(1);
...=penLen * GRAV;
////////////////////
The compiler complained "no operator defined for Acceleration".
If the both oprands have the same L,M,T, it works. But how to deal with
it if they are different?
And the return value is another issue, because it will have another set
of L,M,T.


Sure.
I tried the following:
template <int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
.........
return q;
},

but failed with " error C2995: 'Quantity<L1*L2,M1*M2,T1*T2> operator
*(const Quantity<L1,M1,T1> &,const Quantity<L2,M2,T2> &)' : template
function has already been defined".


Wrong compiler, maybe?
--------------------------------------- This:
template <int L, int M, int T>
class Quantity
{
public:
Quantity(double = 0);
template<int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2);
};

template<int L1, int M1, int T1, int L2, int M2, int T2>
Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1, const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
return q;
}

typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;

const Acceleration GRAV(9.81);
const Length penLen(1);

int main()
{
Quantity<2,0,-2> q = penLen * GRAV;
}
------------------------------------------- compiles fine with Comeau.

VC++ v8 chokes on it, but it's not something unexpected, really. MS'
compiler has been having troubles with templates and today isn't the
last day of it. I'll ask in microsoft.public.vc.language. Maybe they
already know of a bug reported on this...

V


Oct 14 '05 #5
Atlas wrote:
Thanks so much for so many work. But I still can solve it. I tried your
codes in MSVC2003 but failed with "internal compiler error".

P.S. operator* has to be friend, to conform the internal type
convention.


First, don't top post (i.e. putting your reply above the post you're
replying to). It's considered impolite.

Please post enough code to demonstrate your problem.

Cheers! --M

Oct 17 '05 #6
OH?I simply use the facility of google groups. If anything wrong,
that's google's convention.
On the other hand, I think my codes are enough for this problem. Other
codes may confuse you. Victor's codes are google example to expain my
question.
Thank you anyway.

Oct 17 '05 #7
This time, the quoted text simply disappeared!

Oct 17 '05 #8
Atlas wrote:
OH?I simply use the facility of google groups. If anything wrong,
that's google's convention.
Incorrect. All you need to do is click on "show options", which you did
above in responding to Victor, and move the cursor *below* the post
you're quoting, which you did not do when responding to Victor. I'm
presently using google, too, and I am perfectly able to follow proper
netiquette with it.
On the other hand, I think my codes are enough for this problem. Other
codes may confuse you.
If you think the posted code is sufficient, that's your call, but don't
be surprised if you get no further responses. See this FAQ for tips on
posting code to this newsgroup:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Victor's codes are google example to expain my question.


Huh?

Cheers! --M

Oct 17 '05 #9
Atlas wrote:
This time, the quoted text simply disappeared!


That's because you clicked "Reply" below the message instead of
clicking "show options" and then "Reply".

Cheers! --M

Oct 17 '05 #10
mlimber wrote:
Victor's codes are google example to expain my question.

Huh?


Well, if you take a look at my previous post, I gave a complete program
that is, in fact, well-formed but fails to compile with, say, VC++. That
is what the OP is talking about, I suppose. I based it on the OP's code
fragment.

BTW, I submitted the same code in a bug report to MS.

V
Oct 17 '05 #11
Victor Bazarov wrote:
mlimber wrote:
Victor's codes are google example to expain my question.

Huh?


Well, if you take a look at my previous post, I gave a complete program
that is, in fact, well-formed but fails to compile with, say, VC++. That
is what the OP is talking about, I suppose. I based it on the OP's code
fragment.

BTW, I submitted the same code in a bug report to MS.

V


And you did fine work, if you don't mind me saying. (My confusion was
more grammatical and semantic than contextual, however.)

Cheers! --M

Oct 17 '05 #12

mlimber wrote:
Victor Bazarov wrote:
mlimber wrote:
>Victor's codes are google example to expain my question.
Huh?


Well, if you take a look at my previous post, I gave a complete program
that is, in fact, well-formed but fails to compile with, say, VC++. That
is what the OP is talking about, I suppose. I based it on the OP's code
fragment.

BTW, I submitted the same code in a bug report to MS.

V


And you did fine work, if you don't mind me saying. (My confusion was
more grammatical and semantic than contextual, however.)

Cheers! --M

Victor is correct. That's what I want to solve.
And mlimber, I think you should submit a bug report to google groups.
Finally, thanks everybody.

Oct 20 '05 #13

Victor Bazarov wrote:
Atlas wrote:
I implemented a template as:
template <int L, int M, int T>
class Quantity
{
....
public:
friend Quantity operator*(const Quantity& q1,const Quantity& q2);


You probably want to drop this declaration from here. Does it really
need to be a friend? If so, you might want to make _all_ 'operator*'
templates friends of all the classes 'Quantity<>':

template<int L1, int M1, int T1, ...> // just like you did below
friend Quantity<L1+L2,M1+M2,T1+T2> operator*( ...
....
};

when I tried to use it as:
////////////////
typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;
const Acceleration GRAV(9.81);
const Length penLen(1);
...=penLen * GRAV;
////////////////////
The compiler complained "no operator defined for Acceleration".
If the both oprands have the same L,M,T, it works. But how to deal with
it if they are different?
And the return value is another issue, because it will have another set
of L,M,T.


Sure.
I tried the following:
template <int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
.........
return q;
},

but failed with " error C2995: 'Quantity<L1*L2,M1*M2,T1*T2> operator
*(const Quantity<L1,M1,T1> &,const Quantity<L2,M2,T2> &)' : template
function has already been defined".


Wrong compiler, maybe?
--------------------------------------- This:
template <int L, int M, int T>
class Quantity
{
public:
Quantity(double = 0);
template<int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2);
};

template<int L1, int M1, int T1, int L2, int M2, int T2>
Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1, const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
return q;
}

typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;

const Acceleration GRAV(9.81);
const Length penLen(1);

int main()
{
Quantity<2,0,-2> q = penLen * GRAV;
}
------------------------------------------- compiles fine with Comeau.

VC++ v8 chokes on it, but it's not something unexpected, really. MS'
compiler has been having troubles with templates and today isn't the
last day of it. I'll ask in microsoft.public.vc.language. Maybe they
already know of a bug reported on this...

V


I modified your codes and it passed.
------------------
#include <iostream>
using namespace std;

template <int L, int M, int T>
class Quantity
{
public:
Quantity(double d=0){};
template<int L1, int M1, int T1, int L2, int M2, int T2>
friend Quantity operator*
(const Quantity& q1,const Quantity& q2);
};

template<int L1, int M1, int T1, int L2, int M2, int T2>
Quantity<L1+L2,M1+M2,T1+T2> operator*
(const Quantity<L1,M1,T1>& q1,const Quantity<L2,M2,T2>& q2)
{
Quantity<L1+L2,M1+M2,T1+T2> q;
return q;
};

typedef Quantity<1,0,-2> Acceleration;
typedef Quantity<1,0,0> Length;

int main()
{
const Acceleration GRAV(9.81);
const Length penLen(1);
Quantity<2,0,-2> q(9);
q=GRAV*penLen;
return 0;
}
----------------------
The important change is the function declaration in the class:
friend Quantity operator*
(const Quantity& q1,const Quantity& q2);
We don't need to specify the template parameter here because we've
already done so in the class template declaration, and to the compiler,
all those Quantity<L1+L2,M1+M2,T1+T2>, Quantity<L2,M2,T2>.. are the
same as Quantity<L,M,T>.

Oct 20 '05 #14

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

Similar topics

9
2727
by: Alexander Stippler | last post by:
Hi, I have got a question concerning the overload resolution rules of C++ and enumeration types. The following little test program illustrates the situation: enum { red, green, blue }; ...
5
5000
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
2
2151
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
18
2427
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
2
1474
by: paolo.dx | last post by:
Hi, I have to overload operator= to call a method of a class, this is what I need. template<class I> class port{ I dato; public: void write(I new_dato){ dato = new_dato;
1
2743
by: amhoov | last post by:
Hi all, OS - MacOS X 10.4.9 Compiler - gcc 4.0 (XCode) I'm *extremely* new to C++ (but have extensive experience with Java), so please go easy on me. I'm running into a very odd situation...
5
2592
by: phiefer3 | last post by:
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features. First of all, I'm using MSVS...
12
2430
by: terminator | last post by:
the following compiles unless the first line is uncommented . when I try to uncomment the first line I get: error : 'B &FM::operator ,(FM::mystruct<A>,B &)' : could not deduce template argument...
1
1724
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
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
7229
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...
1
6905
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
5485
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
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.