Operator Overloading

Started by
4 comments, last by Serano 1 19 years, 7 months ago
This code is giving me two errors that I just can't work out.

#include <iostream>
using namespace std;

class cBox       
{
public:
	cBox(double iLength = 2.0, double iWidth = 2.0, double iHeight = 2.0):
	                                                                    Length(iLength),
																		Width(iWidth),
																		Height(iHeight)
																		{ };
																	                                                                      
	cBox(const cBox& aBox);    
	~cBox();                   
	
    bool operator>(const cBox& aBox) const
	{
		return (this->Volume()) > (aBox.Volume()); 
	}

	bool operator>(const double& Value) const
	{
		return (this->Volume()) > Value;
	}

	inline double Volume() const
	{
		return Height * Width * Length;
	}

private:
	double Height;
	double Width;
	double Length;	
};

cBox::cBox(const cBox& aBox)
{
	Height = aBox.Height;
	Length = aBox.Length;
	Width = aBox.Width;
}

cBox::~cBox() { }
bool operator>(const double& Value, const cBox aBox);


int main(void)
{
	cBox SmallBox(2.0, 2.0, 2.0);
	cBox MediumBox(4.0, 4.0, 4.0);
	cBox LargeBox(10.0, 10.0, 10.0);

	if (MediumBox > SmallBox)
		cout << "Medium is larger than Small" << endl;
	else
		cout << "Small is larger than Medium" << endl;

	if (SmallBox > 50.0)
		cout << "Small box is larger than 50" << endl;
	else
		cout << "Small box is less than 50" << endl;

	if(50.0 > MediumBox)
		cout << "50 is greater than medium box" << endl;
	else 
		cout << "50 is less than medium box" << endl;

	return 0;
}



The erorrs im getting are
Quote: --------------------Configuration: complete op overload - Win32 Debug-------------------- Compiling... Main.cpp Linking... Main.obj : error LNK2001: unresolved external symbol "bool __cdecl operator>(double const &,class cBox)" (??O@YA_NABNVcBox@@@Z) Debug/complete op overload.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. complete op overload.exe - 2 error(s), 0 warning(s)
Iv'e tried google and msdn and I can't find an answer. [EDIT] Sorry about the formmating in the Source tags. I don't know how to make it better.
Advertisement
bool operator>(const double& Value, const cBox aBox);

Only declares an operator, you don't give an implementation, that is what the compiler is complaining about.
Look at this line of code:

if(50.0 > MediumBox)


Which object will actually have operator>() called on it? (hint: it's the one on the left)

Which operator>() do you actually define? (hint: not the one the compiler is looking for)

enum Bool { True, False, FileNotFound };
Ahh, stupid mistake.
Thank you both for the quick replies.

However, I've now got another problem with the code.
More specificly, the default constructor. If I initialise a cBox with the default constructer eg/
cBox SmallBox();


I get the following errors.
Quote:
C:\Program Files\Microsoft Visual Studio\MyProjects\complete op overload\Main.cpp(54) : error C2679: binary '>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\complete op overload\Main.cpp(59) : error C2446: '>' : no conversion from 'const double' to 'class cBox (__cdecl *)(void)'
There is no context in which this conversion is possible
C:\Program Files\Microsoft Visual Studio\MyProjects\complete op overload\Main.cpp(59) : error C2115: '>' : incompatible types
Error executing cl.exe.


To me this is strange because I thought that it would just use the default values.
When you use a default constructor that takes no parameters, do not put parentheses in the code.

For instance
cBox smallBox(); // this line declares a function called smallBox //which takes no parameters and returns an object of type cBox.  //This was not your intention.cBox smallBox; // this creates a default cBox object
Thanks alot, again a stupid mistake on my part.

This topic is closed to new replies.

Advertisement