Overloading Assignment Operator

I need help overloading the assignment operator

Page 1 of 1

2 Replies - 1729 Views - Last Post: 10 October 2009 - 09:32 PM Rate Topic: -----

#1 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Overloading Assignment Operator

Post icon  Posted 09 October 2009 - 08:41 PM

Ok so baisically I have to write a tuple class that will. And we are required to to define operators for it. I assume that my other methods are working although I haven't tested it yet. I'm getting an error where when i tried to store the result of addition of two of my objects. For example
Tuple1 = tuple2 + tuple3	   //This would give an error




The error states
1>g:\tuplemain.cpp(21) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'double *' (or there is no acceptable conversion)

So my question is, How would i go about overloading the assignment operator where i can do an assignment
of both the left and the right hand side.


This is the header file, Tuple.h
namespace math
{

	class Tuple
	{
	public:
		Tuple();
		Tuple(int size);
		Tuple(double[],int);
		Tuple(const Tuple&);
		double getTupleSize();
		void setTupleSize();
		double* operator +(const Tuple&);
		double* operator -(const Tuple&);
		double* operator /(const double);
		double* operator * (const Tuple&);
		double* operator * (const double);
		Tuple operator =(const Tuple&);
	
		bool operator !=(const Tuple&);
		bool operator ==(const Tuple&);
		double& operator[](int i);
		const double& operator[](const int& i)const;
		const Tuple operator [](const Tuple&)const;
		Tuple* operator *= (const Tuple&);
		Tuple* operator /=(const Tuple&);
		double length(const Tuple&)const;
		double dot(const Tuple&)const;
		double* additiveInverse()const;
		void printTuple();
		class SizeMisMatch{};
		class OutOfBounds{};

		



	private:
		unsigned  int size;
		double *tuplePtr;
		double *result;
		
		

	
	};



}








This is Tuple.cpp
#include "Tuple.h"
#include<iostream>
#include<cmath>
using namespace std;
using math::Tuple;



Tuple::Tuple():size(0)
{

	tuplePtr = new(nothrow) double[size];

	for(unsigned int i = 0; i< size; i++)
	{
		tuplePtr[i] = 0;

	}

}


Tuple::Tuple(int size)
{
	this->size = size - 1;

	tuplePtr = new(nothrow) double[size];
	result = new double[size]; 


	for(int i = 0; i< size; i++)
	{
		tuplePtr[i] = 0;
	}



}

Tuple::Tuple(double temp[], int size)
{
	this->size = size;						//It's one less because arrays start at slot zero

	tuplePtr = new(nothrow) double[size];
	result = new double[size]; 

	for(int i = 0; i< size; i++)
	{
		tuplePtr[i] = temp[i];

	}

}


Tuple::Tuple(const Tuple&x):tuplePtr(x.tuplePtr), size(x.size){}


double Tuple::getTupleSize()
{
	return size;

}

Tuple Tuple::operator =(const math::Tuple &x)
{
	
	tuplePtr = x.tuplePtr;
	size = x.size;

	return *this;
	
}


double& Tuple::operator [](int i)
{
	if((unsigned) i > size-1)
		throw OutOfBounds();

	return tuplePtr[i];
}



const double& Tuple::operator[](const int& i)const
{
	return tuplePtr[i];
}

double *Tuple::operator +(const Tuple& x)
{

	double xSize = x.size;
	
	
	if(size == xSize)
	{
		for(unsigned int i = 0; i<size; i++)
		{
			result[i] = tuplePtr[i] + x.tuplePtr[i];
		}

	}
	else
		throw SizeMisMatch();


	return result;
}


double *Tuple::operator -(const Tuple& x)
{
	
	double xSize = x.size;
	
	
	if(size == xSize)
	{
		for(unsigned int i = 0; i<size; i++)
		{
			result[i] = tuplePtr[i] - x.tuplePtr[i];
		}

	}
	else
		throw SizeMisMatch();


	return result;
}

double*  Tuple::operator /(const double x)
{


	for(unsigned int i = 0; i<size; i++)
	{
		result[i] = tuplePtr[i] / x;
	}


	return result;
}


double* Tuple::operator *(const double x)
{

	for(unsigned int i = 0; i<size; i++)
	{
		result[i] = tuplePtr[i] * x;
	}


		
	return result;


}

bool Tuple::operator!=(const Tuple&x)
{


	if(size != x.size)
		return true;

	return false;

}

double Tuple::dot(const Tuple& x)const
{


	double xSize = size;
	double dotProd = 0;
	
	
	if(size == xSize)
	{
		for(unsigned int i = 0; i<size; i++)
		{
			dotProd += tuplePtr[i] * x.tuplePtr[i];
		}

	}
	else
		throw SizeMisMatch();

	return dotProd;

}


double Tuple::length(const Tuple& x)const
{
	return sqrt(dot(x));
}




void Tuple::printTuple()
{
	cout <<"[ ";

	for(unsigned int i = 0; i<size; i++)
	{
		cout << tuplePtr[i] <<" ";

	}
	cout <<"]";
	cout <<endl;


}



Is This A Good Question/Topic? 0
  • +

Replies To: Overloading Assignment Operator

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Overloading Assignment Operator

Posted 10 October 2009 - 12:00 AM

To the way you defined adition of a tuple plus a tuple the result is a point to a double.

double* operator +(const Tuple&);

so the compiler does not understand why you would do this:
Tuple1 = tuple2 + tuple3

I think what you mean is to represent a tuple as an array of doubles... thus the return type...

You have three choices, but two of them equally problematic.

#1 Create a constructor to convert from a double* to a tuple
#2 Create a cast operator to convert from a double* to a tuple
#3 Change the binary opeartors to:
tuple & operator+(tuple& rhs);

#1 and #2 will get rid of the compiler error because it gives the compiler a route to convert the double* into a tuple... But they are both kind of stupid because you have no way of knowing the size of the tuple!

#3 would probably be a better choice.
Was This Post Helpful? 1
  • +
  • -

#3 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Overloading Assignment Operator

Posted 10 October 2009 - 09:32 PM

thanks I went ahead and changed it, and i seemed liked it worked. Only thing is I have the problem when i set two pointer equal too each other.
tuple1 = tuple2



This code worked before i added a destructor, The only problem was that whenever i would change tuple1 after this assignment, tuple2 would also be changed which shouldn't happen.

After I added a destructor it just gives a runtime error that says
_BLOCK_TYPE_IS_VALID.

I know the problem happens when i set the two pointers equal to each other but i cant figure out a better way to do the assignment statement
Tuple& Tuple::operator =(const math::Tuple &x)
{

	
	tuplePtr = x.tuplePtr;
	size = x.size;


	return *this;

}





Here is my updated tuple.cpp
#include "Tuple.h"
#include<iostream>
#include<cmath>
using namespace std;
using math::Tuple;



Tuple::Tuple():size(0)
{

	tuplePtr = new(nothrow) double[size];

	for(unsigned int i = 0; i< size; i++)
	{
		tuplePtr[i] = 0;

	}

}


Tuple::Tuple(int size)
{
	this->size = size - 1;

	tuplePtr = new(nothrow) double[size];

	for(int i = 0; i< size; i++)
	{
		tuplePtr[i] = 0;
	}



}

Tuple::Tuple(double temp[], int size)
{
	this->size = size;						//It's one less because arrays start at slot zero

	tuplePtr = new(nothrow) double[size];
	 

	for(int i = 0; i< size; i++)
	{
		tuplePtr[i] = temp[i];

	}

}


Tuple::Tuple(const Tuple&x):tuplePtr(x.tuplePtr), size(x.size){}	//copy constructor


double Tuple::getTupleSize()
{
	return size;

}

Tuple& Tuple::operator =(const math::Tuple &x)
{

	
	tuplePtr = x.tuplePtr;
	size = x.size;


	return *this;

}


double& Tuple::operator [](int i)
{
	if((unsigned) i > size-1)
		throw OutOfBounds();

	return tuplePtr[i];
}



const double& Tuple::operator[](const int& i)const
{
	return tuplePtr[i];
}

Tuple& Tuple::operator +(const Tuple& x)
{

	double xSize = x.size;
	
	
	if(size == xSize)
	{
		for(unsigned int i = 0; i<size; i++)
		{
			tuplePtr[i] = tuplePtr[i] + x.tuplePtr[i];
		}

	}
	else
		throw SizeMisMatch();


	return *this;
}


Tuple& Tuple::operator -(const Tuple& x)
{
	
	double xSize = x.size;
	
	
	if(size == xSize)
	{
		for(unsigned int i = 0; i<size; i++)
		{
			tuplePtr[i] = tuplePtr[i] - x.tuplePtr[i];
		}

	}
	else
		throw SizeMisMatch();


	return *this;
}

Tuple&  Tuple::operator /(const double x)
{


	for(unsigned int i = 0; i<size; i++)
	{
		tuplePtr[i] = tuplePtr[i] / x;
	}


	return *this;
}


Tuple& Tuple::operator *(const double x)
{

	for(unsigned int i = 0; i<size; i++)
	{
		tuplePtr[i] = tuplePtr[i] * x;
	}


		
	return *this;


}

Tuple& Tuple::operator *=(const double x)
{
	for(unsigned int i = 0; i<size; i++)
	{
		tuplePtr[i] = tuplePtr[i] * x;
	}

	this->operator =(*this);

	return *this;
}

bool Tuple::operator!=(const Tuple&x)const
{


	if(size != x.size)
		return true;

	return false;

}

bool Tuple::operator ==(const Tuple&x)const
{
	if(size == x.size)
	{
		for(unsigned int i = 0; i < size; i++)
		{
			if(tuplePtr[i] != x.tuplePtr[i])
				return false;
		}

		return true;
	}

	return false;
}

double Tuple::dot(const Tuple& x)const
{


	double xSize = size;
	double dotProd = 0;
	
	
	if(size == xSize)
	{
		for(unsigned int i = 0; i<size; i++)
		{
			dotProd += tuplePtr[i] * x.tuplePtr[i];
		}

	}
	else
		throw SizeMisMatch();

	return dotProd;

}


double Tuple::length(const Tuple& x)const
{
	return sqrt(dot(x));
}




void Tuple::printTuple()const
{
	cout <<"[ ";

	for(unsigned int i = 0; i<size; i++)
	{
		cout << tuplePtr[i] <<" ";

	}
	cout <<"]";
	cout <<endl;


}
Tuple::~Tuple()
{
	delete []tuplePtr;

	tuplePtr = NULL;			//done so tuplePtr can't be pointed too.

	
	

};



My header file
namespace math
{

	class Tuple
	{
	public:
		Tuple();
		Tuple(int size);
		Tuple(double[],int);
		Tuple(const Tuple&);
		double getTupleSize();
		void setTupleSize();
		Tuple& operator +(const Tuple&);
		Tuple& operator -(const Tuple&);
		Tuple& operator /(const double);
		Tuple& operator * (const double);
		Tuple& operator =(const Tuple&);
		Tuple& operator +=(const Tuple&);
		Tuple& operator -=(const Tuple&);
		Tuple& operator *=(const double);
		Tuple& operator *= (const Tuple&);
		Tuple& operator /=(const double);
		bool operator !=(const Tuple&)const;
		bool operator ==(const Tuple&)const;
		double& operator[](int i);
		const double& operator[](const int& i)const;
		const Tuple operator [](const Tuple&)const;
		double length(const Tuple&)const;
		double dot(const Tuple&)const;
		double* additiveInverse()const;
		void printTuple()const;
		class SizeMisMatch{};
		class OutOfBounds{};
		~Tuple();
	

		



	private:
		unsigned  int size;
		double *tuplePtr;
		
		

	
	};



}


This post has been edited by Zerobu: 10 October 2009 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1