operator+

Started by
6 comments, last by TheSorcerer 20 years, 7 months ago
I have a this code:

#include <iostream>
#include <iomanip>

using namespace std;

class lang
{
public:
	lang(unsigned long u = 0){hoog = 0; laag = u;}
	friend lang operator+(const lang &x, const lang &y);  // Line 10

	friend ostream &operator<<(ostream &os, const lang &u);
private:
	unsigned long hoog, laag;
};

int main()
{
	return 0;
}
VC6 gives me this error:

Main.cpp(10) : fatal error C1001: INTERNAL COMPILER ERROR
        (compiler file ''msc1.cpp'', line 1786) 
Error executing cl.exe.
 
It works when I change operator+ to operator* , but it also fails when I change operator+ to operator- . Anyone has a though about it?
Advertisement
I think it is a bug of the VC6.
INTERNAL COMPILER ERROR is not the problem of your code.

Last update of the VC6 may correct it.
Check microsoft page if there is such a bug exists.
be yourself.
TheSorcerer

Why do you have operator+ as a friend with two arguments

should be
lang operator+(const lang &x)
Have it create a temp lang object that has valuse of the this (class values) + x and return back the temp lang object.

Lord Bart

quote:Original post by Lord Bart
Why do you have operator+ as a friend with two arguments

should be
lang operator+(const lang &x)
Have it create a temp lang object that has valuse of the this (class values) + x and return back the temp lang object.

No, it shouldn't. As the constructor is implicit (it has one formal parameter, and is not declared as explicit) it can be used as a conversion. For example, here:

lang one;
...
lang two = 2 + one; // yes, I know I didn't use `2UL', but this is just an example and it can be implicitly converted

the addition can be done, as 2 is converted to a temporary lang object (because of lang's constructor), and then the global (friend) addition operator can be called, because the operands are of the correct type. If it were defined as a member function, the operator+() funtion would have to be invoked by a lang object, but the literal `2' is of type int, and int (or anything that it can implicitly converted to) doesn't have a built-in addition operator that takes a right hand operator of type lang. Otherwise, you'd have to use an explicit cast. Implicit conversions can generally cause bugs that are hard to trace, so one should be careful.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 2, 2003 1:09:45 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Hello Lektrix,

I see, but not quite understand it fully.
For one TheSorcerer never said he was going to use it the way you state. Since if you don''t declare a copy constructor or an assignment operator the compiler will create one for you.

For two I would have to say that looking a lang two = 2 + one is not very intuitively to understand what is going on.

You say that when you have lang two = 2 + one that implicitly converted, is it the 2 that is which I can see that.
does lan two = 2 + one => two(2) + one;
or is it suppose to be lang two( 2 + one);

if = is a short hand for a constructor then it should be
lang two = 2 + one => lang two ( 2 + one )
If it the 2nd case the compiler is going to convert 2 into a lang object and then call the friend function. All under this assumption: lang two ( 2+one) turns into two ( lang(2) + one).

Now if compiler can do this to 2 then it should have no problem then doing this through operator member method either. Once the 2 is converted into a lang object then + method with one can be use and then the return object assign to two.

Or am I missing something here, which could be total true

But just rewrite lang two = one + 2 and you get same thing with my suggestion. Since then compile would convert 2 into a lang and then + operator of one is call and then it assign to two.

Just try to figure your explanation out.

Lord Bart

PS I not sure if I explain my part well either
quote:For one TheSorcerer never said he was going to use it the way you state.

You're missing the point, which is that you said that the OP 'should' have done something, when they certainly didn't have to. I was merely correcting.
quote:Since if you don't declare a copy constructor or an assignment operator the compiler will create one for you.

What does that have to do with anything?
quote:For two I would have to say that looking a lang two = 2 + one is not very intuitively to understand what is going on.

Once again, that's not the point.
quote:
Now if compiler can do this to 2 then it should have no problem then doing this through operator member method either. Once the 2 is converted into a lang object then + method with one can be use and then the return object assign to two.

Did you even try it out with a compiler (or compilers)? The compiler will not attempt to convert the literal `2' to a lang object. Conversions take place for member function arguments, not for member function invokers. The C++ Standard would probably have more to say about this. You can purchase it for something like 18USD. As I said in my previous post, though, you could use an explicit cast for the conversion:

lang two = 2 + one; // won't work if `operator+()' is a member function
lang two = lang(2) + one; // will work

quote:But just rewrite lang two = one + 2 and you get same thing with my suggestion. Since then compile would convert 2 into a lang and then + operator of one is call and then it assign to two.

Yes, I'm merely pointing it out. It makes for a more flexible class.

Sorry, I had a few troubles understanding your post. Are you a native English speaker, or maybe you are just typing a little too fast? :]

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 2, 2003 3:13:20 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Hello Lektrix,

Yes born in USA, but have a few problems.
One I dyslexic and always swap letters around or using wrong simluar sounding or spell words. like form and from I get mix up all the time.
Two fast typer which also adds to spelling errors, typos, missed letter.
I really talk a hell of a lot better then I write.

Thanks for explaining again I now get where you were coming form.

Most of the time I do try to make short test cases of code people post but I did not in this case.

Lord Bart
Sorry, I hope you didn''t take that as an insult. I was just apologising in advance for if I had misinterpreted what you had said.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement