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. 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
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
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
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
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
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.
This time, the quoted text simply disappeared!
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
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
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
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
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.
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>. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 };
...
|
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,...
|
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...
|
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
|
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;
| |
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...
|
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...
|
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...
|
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 =...
|
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,...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
| |