473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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::operator + ( 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.
Aug 29 '07 #1
8 2692
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.
Aug 29 '07 #2
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::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::operator+ ( 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.
Aug 29 '07 #3
"Skybuck Flying" <sp**@hotmail.comwrote in message
news:fb**********@news6.zwoll1.ov.home.nl...
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::operator + ( const TSkybuckInt32& A, const
TSkybuckInt32& B);
This actually should be now:
TSkybuckInt32 TSkybuckInt32::operator + ( 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>
--
Abdo Haji-Ali
Programmer
In|Framez
Aug 29 '07 #4
Ok,

It's working now:

Thanks to this example :)

http://groups.google.nl/group/alt.co...345ff0eb80d2d4

// 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::operator+ ( 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.
Aug 29 '07 #5

"Skybuck Flying" <sp**@hotmail.comwrote in message
news:fb**********@news6.zwoll1.ov.home.nl...
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.

--
Abdo Haji-Ali
Programmer
In|Framez
Aug 29 '07 #6
(The code is now working see other thread follow up)

Question for you:

What is the difference between:

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

and

TSkybuckInt32 TSkybuckInt32::operator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}
Only thing different: &

Bye,
Skybuck.
Aug 29 '07 #7
Skybuck Flying wrote:
(The code is now working see other thread follow up)

Question for you:

What is the difference between:

TSkybuckInt32& TSkybuckInt32::operator+ ( const TSkybuckInt32&
ParaSkybuckInt32 )
{
mInteger = mInteger + ParaSkybuckInt32.mInteger;
return *this;
}
This is return by reference.
and

TSkybuckInt32 TSkybuckInt32::operator+ ( 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
Aug 29 '07 #8
On 2007-08-29 15:43, Skybuck Flying wrote:
(The code is now working see other thread follow up)

Question for you:

What is the difference between:

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

and

TSkybuckInt32 TSkybuckInt32::operator+ ( 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.

--
Erik Wikström
Aug 29 '07 #9

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

Similar topics

4
by: Mohammad | last post by:
I'm implementing a c++ template class CScan to minipulate a series of numbers. I have implemented operator() to select a range of numbers it works fine for an expression like: scan1 = scan2(0,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
10
by: Christian Christmann | last post by:
Hi, I added an operator overloader in my template class: graph.h: template <class NODE> class Node {
3
by: Tony Johansson | last post by:
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...
12
by: Raghu | last post by:
Hello all, I need your help on how to redefine teh function of arithmetic operators in C. for example : if there is an equation c = a/b; i want to execute my own algorithm for division...
10
by: olson_ord | last post by:
Hi, I am not exactly new to C++, but I have never done operator overloading before. I have some old code that tries to implement a Shift Register - but I cannot seem to get it to work. Here's a...
1
by: Satpreet | last post by:
I'm looking to simulate the behavior of a digital hardware arithmetic block in a C/C++ program. I was just wondering if there are any libraries (with datatypes and overloaded operators etc.)...
3
by: Binary | last post by:
Hi, If there is operator overloading in both side of the binary operator, such as: a < b. Will the a's operator function be called or b? Thanks. ABAI
2
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.