operator overloading

T

Tony Johansson

Hello!

I have this wrapper class Integer below that I use when testing operator
overloading. But I run into problem.
First this friend definition below that I have within class definition cause
the following compile error se below. I can't understand why.
Second in main when I for example use this I get completely wrong value in
object k. I should get 5.
int main()
{
Integer i(2);
Integer j(3);
Integer k = i+j;
cout << k.get();
return 0;
}

friend Integer operator+(int v, const Integer& i)
{
Integer local(v + i.value_);
return local;
}

Compiling...
start.cpp
c:\documents and settings\tony\com\slask\integer.h(21) : fatal error C1001:
INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more
information
Error executing cl.exe.

slask.exe - 1 error(s), 0 warning(s)


class Integer
{
public:
explicit Integer(int=0)
{}

int get() const
{ return value_; }

Integer operator+(const Integer& i) const
{
Integer local(value_ + i.value_);
return local;
}

friend Integer operator+(int v, const Integer& i)
{
Integer local(v + i.value_);
return local;
}

private:
int value_;
};

Integer operator+(const Integer& i, int v)
{
Integer local(v + i.get());
return local;
}

//Tony
 
V

velthuijsen

For the compiler error you should go to one of the subgroups
comp.os.ms-windows relating to either the compiler or programming.
You might want to add in a full program from the first include to the
return.
I ran your program on MSVC++ 7.1. No compiler error there.

Oh and that completely wrong value you get. Take a better look at that
very empty constructor you are using.
 
R

Rolf Magnus

Tony said:
Hello!

I have this wrapper class Integer below that I use when testing operator
overloading. But I run into problem.
First this friend definition below that I have within class definition
cause the following compile error se below. I can't understand why.
Second in main when I for example use this I get completely wrong value in
object k. I should get 5.

No. You want to get 5. That's a difference ;-)
int main()
{
Integer i(2);
Integer j(3);
Integer k = i+j;
cout << k.get();
return 0;
}

friend Integer operator+(int v, const Integer& i)
{
Integer local(v + i.value_);
return local;
}

Compiling...
start.cpp
c:\documents and settings\tony\com\slask\integer.h(21) : fatal error
C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more
information
Error executing cl.exe.

slask.exe - 1 error(s), 0 warning(s)


class Integer
{
public:
explicit Integer(int=0)
{}

You never write the int value you got anywhere, so after construction,
value_ is still uninitialized.
 
V

Victor Bazarov

Tony said:
I have this wrapper class Integer below that I use when testing operator
overloading. But I run into problem.
First this friend definition below that I have within class definition cause
the following compile error se below. I can't understand why.
Second in main when I for example use this I get completely wrong value in
object k. I should get 5.
int main()
{
Integer i(2);
Integer j(3);
Integer k = i+j;
cout << k.get();
return 0;
}

friend Integer operator+(int v, const Integer& i)

Drop the keyword 'friend' here. You're only allowed to use it inside
a class definition.
{
Integer local(v + i.value_);
return local;
}
[...]

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top