Operator Overloading, again...

Started by
4 comments, last by WiseElben 20 years, 8 months ago
Sure, Classes is hard (and still learning), Pointers are tricky (Not very good at it yet), but the weirdest of them all is Operator Overloading. I''ve read the GT Operator Overloading tut, but I just can''t seem to get it. Now, for the sake of GDN bandwith, I "tried" to make a simple Operator Overloading program.

#include <iostream>
using namespace std;

class ELBEN
{
public:

	ELBEN():age(13) {  } //Nothing done

	ELBEN(int ageElben):age(ageElben) {  } //Same same...

	ELBEN& operator +(const ELBEN&);
	int age;
};

ELBEN ELBEN::operator +(const ELBEN &temp)
{
	ELBEN result(age + temp.age);
	return result;
}

int main()
{
	ELBEN elben1(13);
	ELBEN elben2(14);
	ELBEN elbenFinal = elben1 + elben2;

	cout << elbenFinal.age;

	return 0;
}
Of coarse, errors appeared. 4, to be exact.
  • c:\Documents and Settings\COMP\My Documents\Visual Studio Projects\Operator Overloading 2\main.cpp(15): error C2556: ''ELBEN ELBEN::operator +(const ELBEN &)'' : overloaded function differs only by return type from ''ELBEN &ELBEN::operator +(const ELBEN &)''
  • c:\Documents and Settings\COMP\My Documents\Visual Studio Projects\Operator Overloading 2\main.cpp(15): error C2040: ''ELBEN::operator`+'''' : ''ELBEN (const ELBEN &)'' differs in levels of indirection from ''ELBEN &(const ELBEN &)''
  • c:\Documents and Settings\COMP\My Documents\Visual Studio Projects\Operator Overloading 2\main.cpp(24): error C2264: ''ELBEN::operator`+'''' : error in function definition or declaration; function not called
  • c:\Documents and Settings\COMP\My Documents\Visual Studio Projects\Operator Overloading 2\main.cpp(24): error C2088: ''+'' : illegal for class
Don''t critize me of the bad coding (ie no .h file, no class decleration file), I''m just trying to be simple. Actually, this program is very poorly coded, very very poorly coded. So if someone is kind enough, would you kindly make a simple program using ONLY one or two operator overloads (The smaller the better)? Thank you. Visit my game programming website at www.wiseelben.com E-mail:wiseelben@wiseelben.com AIM: WiseElben ICQ: 299127026
Advertisement
I''ll attempt some of your problem

This is passing by reference.
ELBEN& operator +(const ELBEN&);

This isn''t
ELBEN ELBEN::operator +(const ELBEN &temp)
{
ELBEN result(age + temp.age);
return result;
}

So just get rid of both & and your code should work.

~~~~~
"That has to be the most impressive collection of blatently incorrect statements I''ve seen on these boards for a while." -benjamin bunny
Download and play Slime King I.
~~~~~Screaming Statue Software. | OpenGL FontLibWhy does Data talk to the computer? Surely he's Wi-Fi enabled... - phaseburn
#include <iostream>using namespace std;class ELBEN{public:	ELBEN():age(13) {  }	ELBEN(int ageElben):age(ageElben) {  }	ELBEN operator +(const ELBEN&) const; // removed an ampersand and made const	int age;};ELBEN ELBEN::operator +(const ELBEN &temp) const{	ELBEN result(age + temp.age);	return result;}int main(){	ELBEN elben1(13);	ELBEN elben2(14);	ELBEN elbenFinal = elben1 + elben2;	cout << elbenFinal.age;	return 0;}
#include <iostream>using namespace std;class ELBEN{public:	ELBEN():age(13) {  } //Nothing done	ELBEN(int ageElben):age(ageElben) {  } //Same same...	ELBEN operator +(const ELBEN&) const;	int age;};ELBEN ELBEN::operator +(const ELBEN &temp){	ELBEN result(age + temp.age);	return result;}int main(){	ELBEN elben1(13);	ELBEN elben2(14);	ELBEN elbenFinal = elben1 + elben2;	cout << elbenFinal.age;	return 0;}


Error: c:\Documents and Settings\COMP\My Documents\Visual Studio Projects\Operator Overloading 2\main.cpp(15): error C2511: ''ELBEN ELBEN::operator +(const ELBEN &)'' : overloaded member function not found in ''ELBEN''




Visit my game programming website at www.wiseelben.com

E-mail:wiseelben@wiseelben.com
AIM: WiseElben
ICQ: 299127026
your problem is very simple ... in your class defintion you have a "const" after the function .. in the actual definition you don''t ..

// I''m omiting the obvious stuffclass ELBEN  {  public:    ELBEN ELBEN::operator +(const ELBEN &temp) const; // see this const  };ELBEN ELBEN::operator +(const ELBEN &temp)  // where''s the const here?  {  ELBEN result(age + temp.age);  return result;  }

Ohh... thanks.



Visit my game programming website at www.wiseelben.com

E-mail:wiseelben@wiseelben.com
AIM: WiseElben
ICQ: 299127026

This topic is closed to new replies.

Advertisement