Jump to content
  • Advertisement
Sign in to follow this  
spree

operator overloading help

This topic is 5395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all. I"ve got an annoying problem with my operators. I'm building a vector class which sepose to have *,/,+,- operators overloaded. But when I"m tring to do something like pos=0.5*x*u*1.0; I get this error: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion) the thing is that my operators (*,/,+,-) always return V2D as a resault, so there cant be any 'double' type left at this expression. Can you guys tell me whats wrong with my operators? main.cpp
#include "V2D.h"

void main()
{
	V2D v(1,2),u(1,3),x(0,0),pos;
	double dt=0.1;

	pos=x*0.5;//ok
	pos=v*u;//ok
	pos=v/u;//ok
	pos=v+u;//ok
	pos=v-u;//ok
	pos=v+0.5;//ok
	pos=0.5+v;//ok

	pos=0.5*x*u*1.0;//This give me an error
	
	cout<<(char*)x;
	
	
}


V2D.h this is the vector class's header file

#ifndef _V2D_H_
#define _V2D_H_


#include <math.h>
#include <assert.h>
#include <iostream>
using namespace std;

#define BUFFER_SIZE 50
class V2D
{
	char *buffer;
	double x,y;

	void copyBuffer(char *);// copys the source to the this->buffer

public:

	V2D();//default constructor
	V2D(double,double);//set x and y
	V2D(V2D &);//copy constructor
	~V2D();//deconstructor

	void setX(double newX);
	void setY(double newY);
	void setXY(double newX,double newY);
	
	double getX();
	double getY();

	void operator= (const V2D &obj);// V2D = V2D
	
	V2D operator- (V2D & ob2);// v=u+x
	friend V2D operator- (V2D &,double scalar);
	friend V2D operator- (double scalar,V2D &);


	V2D operator+ (V2D & ob2);// v=u+x
	friend V2D operator+ (V2D &,double scalar);
	friend V2D operator+ (double scalar,V2D &);

	
	V2D operator* (V2D & ob2);// v=u*x
	friend V2D operator* (V2D &,double scalar);
	friend V2D operator* (double scalar,V2D &);
	

	V2D operator/(V2D &);
	friend V2D operator/ (V2D &,double scalar);
	friend V2D operator/ (double scalar,V2D &);
	


	bool operator ==(V2D &);
	bool operator ==(double);
	bool operator ==(int);

	operator double () const ; // (double) V2D // Returns the SIZE of the vector
	operator char * () const ; // (double) V2D
	
	double size();
	char * toString();
	

};

#endif


this is the V2D.cpp
#include"V2D.h"


//[Help functions]============================================

void V2D::copyBuffer(char *src)
{

		// allocate string buffer and copy the buffer of the obj to this
	buffer = new char[BUFFER_SIZE];
	
}


//[Interface]=================================================
V2D::V2D():x(0.0),y(0.0)
{
	buffer= new char[BUFFER_SIZE];
}
V2D::V2D(double x,double y):x(x),y(y)
{
	buffer= new char[BUFFER_SIZE];
}

V2D::V2D(V2D & obj)// Copy Constructor
{
	// Copy the Vectors attributes
	x=obj.x;
	y=obj.y;

	copyBuffer(obj.buffer);

}
V2D::~V2D()
{	
	delete []buffer;// delete the allocated buffer
}

//[Set Functions]=========================================================

void V2D::setX(double newX)
{
	x=newX;
}
void V2D::setY(double newY)
{
	y=newY;
}

void V2D::setXY(double newX,double newY)
{
	setX(newX);
	setY(newY);
}
//[Get Functions]=========================================================
double V2D::getX()
{
	return x;
}

double V2D::getY()
{
	return y;
}

char * V2D::toString()
{
	return (char*)(*this);// we have a (char*) cast operator overloaded
}

//[Operators]==========================================================




void V2D::operator = (const V2D &obj)
{
	x=obj.x;
	y=obj.y;
	
	copyBuffer(obj.buffer);
}
//[operator ==]=============================================
bool V2D::operator ==(V2D &v)
{
	return (x==v.x && y==v.y );
}
bool V2D::operator ==(double c)
{
	return (x==c && y==c );
}
bool V2D::operator ==(int c)
{
	return (x==c && y==c );
}


//[Cast operator (double) Returns the SIZE of the vector |v|
V2D::operator double() const
{
	return sqrt(x*x+y*y);
}

double V2D::size()
{
	return (double)(*this);// we have a (double) cast operator overloaded, which returns the size of th evector
}

V2D::operator char * () const // return a strinf of the vector
{
	sprintf(buffer,"(%f , %f)",x,y);
	
	return buffer;
}

//[+ operator]===============================================================
V2D V2D::operator + (V2D &ob2) // V2D=V2D+V2D
{
	return V2D(x+ob2.x,y+ob2.y);
}
V2D operator+(V2D &v,double scalar) // v= v*1.0
{
	return V2D(v.x+scalar,v.y+scalar);
}

V2D operator+(double scalar,V2D &v) // v= 1.0*v
{
	return V2D(v.x+scalar,v.y+scalar);
}

//[- operator]===============================================================
V2D V2D::operator - (V2D &ob2) // V2D=V2D+V2D
{
return V2D(x-ob2.x,y-ob2.y);
}
V2D operator-(V2D &v,double scalar) // v= v*1.0
{
	return V2D(v.x-scalar,v.y-scalar);
}

V2D operator-(double scalar,V2D &v) // v= 1.0*v
{
	return V2D(v.x-scalar,v.y-scalar);
}


//[* operator]===============================================================

V2D V2D::operator *(V2D &u)
{
	return V2D(x*u.x,y*u.y);
}
V2D operator*(V2D &v,double scalar) // v= v*1.0
{
	return V2D(v.x*scalar,v.y*scalar);
}

V2D operator*(double scalar,V2D &v) // v= 1.0*v
{
	return V2D(v.x*scalar,v.y*scalar);
}

//[operators]===========================================================

V2D V2D::operator /(V2D &v)
{
		assert(v.x!=0);// Debug 
		assert(v.y!=0);// Debug 
	
	try // see if there is a divide by zero attempt
	{
		if(v.x==0 || v.y==0) throw 1;
	}catch(...)
	{
		
		MessageBox(NULL,"V2D class: attempt to devide by zero","Exeption",MB_OK);
		
	}
return V2D(x/v.x,y/v.y);
}
V2D operator/(V2D &v,double scalar)
{
	assert(scalar!=0);// Debug 
	
	try // see if there is a divide by zero attempt
	{
		if(scalar==0) throw 1;
	}catch(...)
	{
		
		MessageBox(NULL,"V2D class: attempt to devide by zero","Exeption",MB_OK);
		
	}
return V2D(v.x/scalar,v.y/scalar);
}


V2D operator/(double scalar,V2D &v)
{
	assert(scalar!=0);// Debug 
	
	try // see if there is a divide by zero attempt
	{
		if(scalar==0) throw 1;
	}catch(...)
	{
		
		MessageBox(NULL,"V2D class: attempt to devide by zero","Exeption",MB_OK);
		
	}
return V2D(v.x/scalar,v.y/scalar);
}



Share this post


Link to post
Share on other sites
Advertisement
The problem is that your operators take the vector arguments as non-const references, and a temporary value (for example the result of 0.5*x) cannot be passed as a non-const reference. Replacing every
V2D &

in your argument lists with
const V2D &

should do the trick.

Share this post


Link to post
Share on other sites
Thanks it worked :)

you mean that if i make a function which takes a none const value, will be given a const value, it wont work ? Did i get it right?

Share this post


Link to post
Share on other sites
Quote:
Original post by spree
you mean that if i make a function which takes a none const value, will be given a const value, it wont work ? Did i get it right?


No, if you have non-constant reference you cannot assign literals or temporaries (some older compilers let you do this but this has undefined behaviour so don't!) only l-values (an object who's address you can take), how-ever for constant references they work a little differently and you can assign literals & temporaries to them aswell as l-values.

Share this post


Link to post
Share on other sites
EDIT: modified and extended my original post which was a little off...
---

In addition to what snk_kid said, const variables cannot be passed as non-const reference, either (constness would have little meaning in the first place if const variables could be modified through a reference, now wouldn't it?)

BTW, there seems to be two differing definitions for the term 'lvalue':

First, the snk_kid's one: something whose address you can take. By this definition, a const variable is an lvalue (some sources then divide lvalues to const and non-const ones)

Second, the definition which the word lvalue is based on: something that can appear as the left-hand side of an assignment operator. By this definition, a const variable would instead not be an lvalue.

[Edited by - Sharlin on October 14, 2004 9:29:46 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
Second, the definition which the word lvalue is based on: something that can appear as the left-hand side of an assignment operator. By this definition, a const variable would instead not be an lvalue.


A const-qualified type is always an lvalue but is not but not modifiable. That was an old definition that i think was suppose to help understand, apparently:

l-value stands for location value (an addresss)
r-value stands for register value (the actual value)

came from assbemly lingo, to help programmers remember to prevent making subtle logic errors.

rules of l-values & r-values are alittle different between C and C++ standard.

if an l-value is used in the context of an r-value its implicitly converted to an r-value the reverse is not true.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!