Archived

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

elendil67

Overloading Vertex Operators

Recommended Posts

elendil67    122
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.

Share this post


Link to post
Share on other sites
smart_idiot    1298
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]

Share this post


Link to post
Share on other sites
elendil67    122
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 operator

template <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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
vertex vertex::operator +(vertex &x) <- reference

vertex operator +(vertex x); <- no reference

Share this post


Link to post
Share on other sites
fizban75    130
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]

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
elendil67    122
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"


// constructor

template <class tvector> vector <tvector> ::vector()
{

}

// overloaded constructor

template <class tvector> vector <tvector> ::vector(tvector tx, tvector ty, tvector tz)
{
// assign the vector

x = tx;
y = ty;
z = tz;
}

// overloaded assignment operator

template <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 operator

template <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]

Share this post


Link to post
Share on other sites
fizban75    130
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!


// constructor

template <class tvector> vector <tvector> ::vector()
{ }

// overloaded constructor

template <class tvector> vector <tvector> ::vector(tvector tx, tvector ty, tvector tz)
{
// assign the vector

x = tx;
y = ty;
z = tz;
}

// overloaded assignment operator

template <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 operator

template <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));
}

Share this post


Link to post
Share on other sites
Nypyren    12074
adding and subtracting other vectors

multiplying and dividing by scalars

getting the length, and the length squared (yes, as separate functions)

performing dot-product, cross-product, and projection (use named functions instead of overloading an operator like % like I did once... made it unreadable )

if you have the above, normalizing is just:

v /= v.length();

so I wouldn''t bother writing a separate "Normalize" function.

Share this post


Link to post
Share on other sites