Binary Arithmetic Add Operator Overloading not compiling, what's wrong ?

S

Skybuck Flying

Hello,

Visual Studio .Net 2005 (Win32) Compile error:

Error 1 error C2804: binary 'operator +' has too many parameters <snipped>
line 16

class TSkybuckInt32
{
private:
int mInteger;
public:

// constructor with initializer parameter
TSkybuckInt32( int ParaValue );

// binary arithmetic add operator overloader
// adds A and B together and returns a new C
TSkybuckInt32 operator+( const TSkybuckInt32& A, const
TSkybuckInt32& B );

void Display();
};


// constructor
TSkybuckInt32::TSkybuckInt32( int ParaValue )
{
mInteger = ParaValue;
}

// binary arithmetic add operator overloader
TSkybuckInt32 TSkybuckInt32::eek:perator + ( const TSkybuckInt32& A, const
TSkybuckInt32& B);
{
TSkybuckInt32 C;
C.mInteger = A.mInteger + B.mInteger;

return C.mInteger;
}

TSkybuckInt32::Display()
{
cout << mInteger << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
TSkybuckInt32 A;
TSkybuckInt32 B;
TSkybuckInt32 C;

A = TSkybuckInt32( 30 );
B = TSkybuckInt32( 70 );
C = TSkybuckInt32( 0 );

C = A + B;
C.Display();

return 0;
}

What's wrong ?

Bye,
Skybuck.
 
S

Skybuck Flying

Maybe it needs to be a public function.

All the C++ references I found via google are not working, half-baked or not
C++.

All the C++ example I found via google are half-baked, they don't show the
complete solution.

Trial and error is the way go apperenetly:

Compiles a bit further:

Now it complains about non-accessable mInteger member... kinda weird...

How to make mInteger accessable to the public function without making
mInteger public ???

Otherwise I might need a different solution I do not want to make mInteger
public if possible...

// TestWritingScalableSoftware.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
class TSkybuckInt32
{
private:
int mInteger;
public:
// constructor with initializer parameter
TSkybuckInt32( int ParaValue );
void Display();
};
// binary arithmetic add operator overloader
// adds A and B together and returns a new C
TSkybuckInt32 operator + ( const TSkybuckInt32 &A, const TSkybuckInt32 &B );

// constructor
TSkybuckInt32::TSkybuckInt32( int ParaValue )
{
mInteger = ParaValue;
}

// add operator overloader
TSkybuckInt32 operator + ( const TSkybuckInt32& A, const TSkybuckInt32& B)
{
TSkybuckInt32 C = TSkybuckInt32( 0 );
C.mInteger = A.mInteger + B.mInteger;
return C.mInteger;
}
TSkybuckInt32::Display()
{
cout << mInteger << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
TSkybuckInt32 A;
TSkybuckInt32 B;
TSkybuckInt32 C;
A = TSkybuckInt32( 30 );
B = TSkybuckInt32( 70 );
C = TSkybuckInt32( 0 );
C = A + B;
C.Display();

return 0;
}

Bye,
Skybuck.
 
S

Skybuck Flying

C++ is wacky:

Tried it all kinds of things not is working:

Error 1 error C4430: missing type specifier - int assumed. Note: C++ does
not support default-int 15

Might have to dig up my old college sources to see a good example.

// TestWritingScalableSoftware.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
class TSkybuckInt32
{
private:
int mInteger;
public:
// constructor with initializer parameter
TSkybuckInt32( int ParaValue );
// first solution:
TSkybuckInt32::eek:perator+( const TSkybuckInt32& ParaSkybuckInt32 );
void Display();
};
// binary arithmetic add operator overloader
// adds A and B together and returns a new C
// this might not be what I want disabled for now
/*
TSkybuckInt32 operator + ( const TSkybuckInt32 &A, const TSkybuckInt32 &B );
*/
// constructor
TSkybuckInt32::TSkybuckInt32( int ParaValue )
{
mInteger = ParaValue;
}

// add operator overloader
/*
TSkybuckInt32 operator + ( const TSkybuckInt32& A, const TSkybuckInt32& B)
{
TSkybuckInt32 C = TSkybuckInt32( 0 );
C.mInteger = A.mInteger + B.mInteger;
return C.mInteger;
}
*/
// first solution:
TSkybuckInt32::eek:perator+ ( const TSkybuckInt32& ParaSkybuckInt32 )
{
Self.mInteger = Self.mInteger + ParaSkybuck.mInteger;
}

TSkybuckInt32::Display()
{
cout << mInteger << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
TSkybuckInt32 A;
TSkybuckInt32 B;
TSkybuckInt32 C;
A = TSkybuckInt32( 30 );
B = TSkybuckInt32( 70 );
C = TSkybuckInt32( 0 );
C = A + B;
C.Display();

return 0;
}

Bye,
Skybuck.
 
S

Skybuck Flying

Ok,

It's working now:

Thanks to this example :)

http://groups.google.nl/group/alt.c...loading+example&rnum=6&hl=en#39345ff0eb80d2d4

// TestWritingScalableSoftware.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
class TSkybuckInt32
{
private:
int mInteger;
public:
// constructor with initializer parameter
TSkybuckInt32( int ParaValue );
// first solution:
TSkybuckInt32& operator+( const TSkybuckInt32& ParaSkybuckInt32 );
void Display();
};
// binary arithmetic add operator overloader
// adds A and B together and returns a new C
// this might not be what I want disabled for now
/*
TSkybuckInt32 operator + ( const TSkybuckInt32 &A, const TSkybuckInt32 &B );
*/
// constructor
TSkybuckInt32::TSkybuckInt32( int ParaValue )
{
mInteger = ParaValue;
}

// add operator overloader
/*
TSkybuckInt32 operator + ( const TSkybuckInt32& A, const TSkybuckInt32& B)
{
TSkybuckInt32 C = TSkybuckInt32( 0 );
C.mInteger = A.mInteger + B.mInteger;
return C.mInteger;
}
*/
// first solution:
TSkybuckInt32& TSkybuckInt32::eek:perator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}

void TSkybuckInt32::Display()
{
printf( "%d \n", mInteger );
}
int _tmain(int argc, _TCHAR* argv[])
{
// must write code like this to use constructor ? can't just declare a,b,c ?
TSkybuckInt32 A = TSkybuckInt32( 30 );
TSkybuckInt32 B = TSkybuckInt32( 70 );
TSkybuckInt32 C = TSkybuckInt32( 0 );
C = A + B;
C.Display();
while (1)
{
}
return 0;
}

Bye,
Skybuck.
 
S

Skybuck Flying

(The code is now working see other thread follow up)

Question for you:

What is the difference between:

TSkybuckInt32& TSkybuckInt32::eek:perator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}

and

TSkybuckInt32 TSkybuckInt32::eek:perator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}


Only thing different: &

Bye,
Skybuck.
 
A

Abdo Haji-Ali

Skybuck Flying said:
class TSkybuckInt32
{
private:
int mInteger;
public:

// constructor with initializer parameter
TSkybuckInt32( int ParaValue );

// binary arithmetic add operator overloader
// adds A and B together and returns a new C
TSkybuckInt32 operator+( const TSkybuckInt32& A, const
TSkybuckInt32& B );
This should be
TSkybuckInt32 operator+( const TSkybuckInt32& A);
The first operand to the + operator is implied, it's "this".
[code snipped]
TSkybuckInt32 TSkybuckInt32::eek:perator + ( const TSkybuckInt32& A, const
TSkybuckInt32& B);
This actually should be now:
TSkybuckInt32 TSkybuckInt32::eek:perator + ( const TSkybuckInt32& A) // No
';'
{
TSkybuckInt32 C;
TSkybuckInt32 doesn't have a default constructor, this should be:
TSkybuckInt32 C(0);
C.mInteger = A.mInteger + B.mInteger;
C.mInteger = this->mInteger + A.mInteger;
return C.mInteger;
}

TSkybuckInt32::Display()
Where's the return type?
void TSkybuckInt32::Display()
{
cout << mInteger << endl;
}

int _tmain(int argc, _TCHAR* argv[])
There's no _tmain in C++ standard, use main instead:
int main(int argc, char* argv[])
{
TSkybuckInt32 A;
TSkybuckInt32 B;
TSkybuckInt32 C;
All three objects need to be constructed, since (as mentioned) TSkybuckInt32
doesn't have a default constructor:
TSkybuckInt32 A(0);
TSkybuckInt32 B(0);
TSkybuckInt32 C(0);
[code snipped]
What's wrong ?
Everything.
<ot>
It looks like you're using Microsoft Visual Studio. If that's the case then
why don't you try reading MSDN. It provides helpful, simple explaination of
all the errors that might come up.
</ot>
 
A

Abdo Haji-Ali

Skybuck Flying said:
Maybe it needs to be a public function.
It's already a public function. What you did is making it a global function
(Not a member of the class)
All the C++ example I found via google are half-baked, they don't show the
complete solution.
Have you tried the FAQ?
Now it complains about non-accessable mInteger member... kinda weird...
No it's not. operator+ is global now, so it's only naturally it doesn't have
access to TSkybuckInt32's private data members.
How to make mInteger accessable to the public function without making
mInteger public ???
You can make that *global* function a friend of your class.
Otherwise I might need a different solution I do not want to make mInteger
public if possible...
See my other post.
 
A

Alan Woodland

Skybuck said:
(The code is now working see other thread follow up)

Question for you:

What is the difference between:

TSkybuckInt32& TSkybuckInt32::eek:perator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}

This is return by reference.
and

TSkybuckInt32 TSkybuckInt32::eek:perator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}

This is return by value. You'd expect it to use a copy here usually,
however in practice it may or may not do so depending on whether your
compiler elides the copy or not (it is permitted to do so, see return
value optimization).

Alan
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

(The code is now working see other thread follow up)

Question for you:

What is the difference between:

TSkybuckInt32& TSkybuckInt32::eek:perator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}

and

TSkybuckInt32 TSkybuckInt32::eek:perator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}

The latter returns a copy of *this, while the first returns *this, read
up about the differences between references, pointers and values.
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,241
Latest member
Lisa1997

Latest Threads

Top