Advertisement

Overloading Vertex Operators

Started by March 06, 2003 06:02 PM
10 comments, last by elendil67 21 years, 8 months ago
For my vertex class, I want to overload the operators. I am doing it the way I read to, but it will not work when I try to define the operators. For example, I have this in my class declaration:
  
vertex operator =(vertex x);							// overload the assignment operator

vertex operator +(vertex x);							// overload the addition operator

  
And in my class definition, I have this:
  
// overloaded assignment operator

vertex vertex::operator =(vertex x)
{
    // return the assigned vertex

    return(vertex(x.x, x.y, x.z));
}

// overloaded addition operator

vertex vertex::operator +(vertex &x)
{
    // return the added vertex

    return(vertex(x + x.x, y + x.y, z + x.z));
}
  
The class declaration goes smoothly with the compiler, but the definitions are what is screwing the program up. Can someone tell me what I am doing wrong here? If so, that would be wonderful. Also, I was looking at d3dx.h, and I noticed that some of the overloaded operators had const appended to them. I think I will have to look this up in my reference, but what does const do to an operator? Thanks for your patience.
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
x is declared for two different variables. I think the compiler will only let you use the vector x, so you should either change the name of the vector you're adding, or use this->x for the x component of your vector.

And

  vertex vertex::operator =(vertex x){    // return the assigned vertex    return(vertex(x.x, x.y, x.z));}  

should look more like

  vertex vertex::operator =(vertex x){ this->x = x.x; y = x.y; z = x.z; return *this;}  


[edited by - smart_idiot on March 6, 2003 7:25:02 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
Sorry, maybe I should have pointed out that this class is a template class. I''ve changed the code this much so far:

  // overloaded assignment operatortemplate <class tvertex> vertex<tvertex>::operator =(tvertex &x){	// return the assigned vertex	return(vertex(x.x, x.y, x.z));}  

I get the following error:
error C2244: ''vertex::='' : unable to resolve function overload 

I''m completely stumped.
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
vertex vertex::operator +(vertex &x) <- reference

vertex operator +(vertex x); <- no reference
Your operators are completely wrong. Here's how you should code them:


      template<class T>class vertex {// stuff here// operator= returns a reference to *this!vertex<T> &operator=(const vertex<T> &p_rhs) {  x = p_rhs.x;  y = p_rhs.y;  z = p_rhs.z;  return *this;}// operator+ returns a new vertex!vertex<T> operator+(const vertex<T> &p_rhs) {  return vertex<T>(x+p_rhs.x, y+p_rhs.y, z+p_rhs.z);}// other stuff here};      


Notice that operator= returns a reference to *this, and operator+ returns a new vertex. In both cases, you should pass a const reference to the vertex on the right hand side of the equation. Learn the correct syntax for operators before you try coding your own.

[edited by - fizban75 on March 7, 2003 3:18:17 PM]

[edited by - fizban75 on March 7, 2003 3:18:58 PM]
I know this is off topic, but you can''t add vertices.
Advertisement
Who says you can''t? Maybe you want to create a new object by combining two models together. It might look weird, but it''s a possibility. A vertex is just a point in 3D space. You can add points together to create a new point and anything else you can think of. There''s no restrictions to manipulating data...
He probably meant you can add vectors, but not points. The two are used pretty much interchangeably though kdogg since in most applications they are defined the same way.

elendil are you declaring your functions in a header and defining them in a separate source file? Most compilers don''t allow you to do that with templates.

Also as someone pointed out you aren''t declaring the operators properly.
I'm sorry about this, but I really don't get it. I tried to look at the code, but that doesn't work either. Thanks for your input though. I think I am entering it or doing something wrong. I'll just post a code dump here:

    // vertex.h// '//!!! - !!!//' signifies errors #include "headers.h" template <class tvector> class vector{public: 	// vector functions	vector();												// vector constructor	vector(tvector x, tvector y, tvector z);				// overloaded vector constructor 	// overload operators	vector operator =(tvector &x);							// overload the assignment operator	vector operator +(tvector &x);							// overload the addition operator	vector operator -(tvector &x);							// overload the subtraction operator	vector operator *(tvector &x);							// overload the multiplication operator	vector operator /(tvector &x);							// overload the division operator	vector operator ==(tvector &x);							// overload the equality operator	vector operator ++();									// overload the increment operator	vector operator --();									// overload the decrement operator	vector operator +=(tvector &x);							// overload the shorthand addition operator	vector operator -=(tvector &x);							// overload the shorthand subtraction operator	vector operator *=(tvector &x);							// overload the shorthand multiplication operator	vector operator /=(tvector &x);							// overload the shorthand division operator	vector operator >>(int x);								// overload the right shift operator	vector operator <<(int x);								// overload the left shift operator 	// vertex positions	tvector x;												// vector x position	tvector y;												// vector y position	tvector z;												// vector z position};// vertex.cpp:#include "vector.h"  // constructortemplate <class tvector> vector <tvector> ::vector(){ } // overloaded constructortemplate <class tvector> vector <tvector> ::vector(tvector tx, tvector ty, tvector tz){	// assign the vector	x = tx;	y = ty;	z = tz;} // overloaded assignment operatortemplate <class tvector> vector <tvector> ::operator =(tvector &x){	// return the assigned vector	return(vector(x.x, x.y, x.z));} //!!! - vector<tvector>::=' : unable to resolve function overload - !!!// // overloaded addition operatortemplate <class tvector> vector <tvector> ::operator +(tvector &x) //!!! - template definitions cannot nest - !!!//{	// return the added vector	return(vector(x + t.x, y + t.y, z + t.z));} //!!! - vector<tvector>::+' : unable to resolve function overload - !!!//    

Thanks for your patience!

[edited by - elendil67 on March 7, 2003 10:49:24 PM]
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
Here''s the correct code, elendil. Go reread my post about what each operator returns and what it takes as arguments. You didn''t change anything. Plus, you''re confusing your template type "tvector" with you class type "vector" (you''re passing tvector instead of vector to your operators...


      // vertex.h// ''//!!! - !!!//'' signifies errors #include "headers.h" template <class tvector> class vector{public:	// vector functions	vector(); // vector constructor	vector(tvector x, tvector y, tvector z);				// overloaded vector constructor 	// overload operators	vector &operator=(const vector &x); // overload the assignment operator	vector operator+(const vector &x); // overload the addition operator// ALL THE REST OF YOUR OPERATORS NEED TO BE CHANGED SIMILARLY TO THE ABOVE. PLEASE READ UP ON THE CORRECT FUNCTIONS DECLARATIONS FOR EACH OPERATOR!	// vertex positions	tvector x; // vector x position	tvector y; // vector y position	tvector z; // vector z position};// TEMPLATES MUST BE DEFINED ALL IN THE HEADER! YOU CANNOT HAVE A .CPP WHEN DEFINING TEMPLATE CLASSES!// constructortemplate <class tvector> vector <tvector> ::vector(){ } // overloaded constructortemplate <class tvector> vector <tvector> ::vector(tvector tx, tvector ty, tvector tz){	// assign the vector	x = tx;	y = ty;	z = tz;} // overloaded assignment operatortemplate <class tvector>vector<tvector> &vector<tvector>::operator=(const vector &rhs){	// return the assigned vector        x = rhs.x; y=rhs.y; z=rhs.z;        return *this;} // overloaded addition operatortemplate <class tvector> vector<tvector> vector<tvector>::operator+(const vector &rhs){	// return the added vector	return(vector(x + rhs.x, y + rhs.y, z + rhs.z));}  

This topic is closed to new replies.

Advertisement