Jump to content
  • Advertisement
Sign in to follow this  
3Dgonewild

How to split class that uses operator(s)

This topic is 4140 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

Im trying to split the following class into two different files : 1 : with the source (.cpp) 2 : and the header (.h) I've done it many times with normal classes , BUT , this class is much different...it has operators! Can someone tell me how to split the class ? I've tried with the known way but im just getting errors ;/!!!!
class VEC2D
{      
public :
  float x,y;
       VEC2D() {}
       VEC2D(float x1,float y1)
       {
       x=x1;
       y=y1;
       }
VEC2D operator+ (VEC2D v2d)
{
return (VEC2D(v2d.x+x,v2d.y+y));
}
VEC2D operator- (VEC2D v2d)
{
return (VEC2D(v2d.x-x,v2d.y-y));
}
VEC2D operator* (VEC2D v2d)
{
return (VEC2D(v2d.x*x,v2d.y*y));
}
VEC2D operator/ (VEC2D v2d)
{
return (VEC2D(v2d.x/x,v2d.y/y));
}
};

Share this post


Link to post
Share on other sites
Advertisement
Operator overloads are no different from regular member functions. So, in the header:

class VEC2D
{
public :
float x,y;

VEC2D();
VEC2D(float x1,float y1);

VEC2D operator+ (VEC2D v2d)
// Etc.
};

And in the source file:

VEC2D VEC2D::operator+(VEC2D v2d)
{
// ...
}


Note that you should probably use nonmember functions for your binary operators (to be able to handle (float * vector) and (vector * float)) and that you should pass your inputs by constant reference.

Also, next time when you're getting errors... provide them. We can't read your mind.

Share this post


Link to post
Share on other sites
Hi,

also it is good practis to make return values off the operators const bcause what should you want the following for?


VEC2D a,b,c;
..
a + b = c;



In the current version this compiles. With const return values this doesn't compile. And your operators are more like what one is used to by the normal operators.

Share this post


Link to post
Share on other sites


Thanks , but still im getting errors ;/


Header:

class VEC2D
{
public :
float x,y;
VEC2D();
VEC2D(float x1,float y1);
VEC2D operator+ (const VEC2D v2d);
VEC2D operator- (const VEC2D v2d);
VEC2D operator* (const VEC2D v2d);
VEC2D operator/ (const VEC2D v2d);
};




And the source:



VEC2D::VEC2D()
{
}
VEC2D::VEC2D(float x1,float y1)
{
x=x1;
y=y1;
}
VEC2D::VEC2D operator+ (const VEC2D v2d)
{
return (VEC2D(v2d.x+x,v2d.y+y));
}
VEC2D::VEC2D operator- (const VEC2D v2d)
{
return (VEC2D(v2d.x-x,v2d.y-y));
}
VEC2D::VEC2D operator* (const VEC2D v2d)
{
return (VEC2D(v2d.x*x,v2d.y*y));
}
VEC2D::VEC2D operator/ (const VEC2D v2d)
{
return (VEC2D(v2d.x/x,v2d.y/y));
}




errors:


2dvec.cpp: In function `VEC2D operator+(VEC2D)':
2dvec.cpp:12: error: `x' undeclared (first use this function)
2dvec.cpp:12: error: (Each undeclared identifier is reported only once for each function it appears in.)
2dvec.cpp:12: error: `y' undeclared (first use this function)

2dvec.cpp: In function `VEC2D operator-(VEC2D)':
2dvec.cpp:16: error: `x' undeclared (first use this function)
2dvec.cpp:16: error: `y' undeclared (first use this function)

2dvec.cpp: In function `VEC2D operator*(VEC2D)':
2dvec.cpp:20: error: `x' undeclared (first use this function)
2dvec.cpp:20: error: `y' undeclared (first use this function)

2dvec.cpp: At global scope:
2dvec.cpp:23: error: `VEC2D operator/(VEC2D)' must take exactly two arguments
2dvec.cpp: In function `VEC2D operator/(VEC2D)':
2dvec.cpp:24: error: `x' undeclared (first use this function)
2dvec.cpp:24: error: `y' undeclared (first use this function)

make.exe: *** [2dvec.o] Error 1

Execution terminated

Share this post


Link to post
Share on other sites
You didn't define the function correctly, pay attention to how you use the scoping operator. The member function must be scoped to the class it belongs to. Also, you're not passing by const reference yet.

VEC2D VEC2D::operator+(const VEC2D &v2d)
{
//...
}

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
You didn't define the function correctly, pay attention to how you use the scoping operator. The member function must be scoped to the class it belongs to. Also, you're not passing by const reference yet.

VEC2D VEC3D::operator+(const VEC2D &v2d)
{
//...
}


Thanks , now it works like a charm! T H A N K S !!!!!!!!!!!!!!!!!! :) !!


By the way :

Quote:

you should pass your inputs by constant reference.


Like this :

function-_-(const VEC2D v2d);

or this :

function-_-(const VEC2D &v2d);

or...this :

function-_-( VEC2D &v2d);

??

Share this post


Link to post
Share on other sites
A constant reference to T looks like: const T& t

For more information, consider reviewing your C++ book. If you don't have one, consider reading "Thinking in C++" or "C++: A Dialog." Both are freely available online.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
A constant reference to T looks like: const T& t

For more information, consider reviewing your C++ book. If you don't have one, consider reading "Thinking in C++" or "C++: A Dialog." Both are freely available online.


Cool , thanks for clearing it out!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!