Archived

This topic is now archived and is closed to further replies.

WiseElben

Operator Overloading, again...

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

#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;
}

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites
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 stuff

class 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;
}

Share this post


Link to post
Share on other sites