tomuś
tomuś

Reputation: 129

Adding method to library

I've been recently writing simple game in C++ with SFML. Here's my question:

In SFML library there is a template class Vector2<T> (specifically I'd be using Vector2f). Unfortunately, it doesn't have any method to rotate itself, so I came with the idea to write one. But as I wrote:

template<typename T> void Vector2<T>::Rotate(float a);

the compiler says that I can't do things like this:

printable.h:31:53: error: no ‘void sf::Vector2<T>::Rotate(float)’ member function declared in class ‘sf::Vector2<T>’

Is it possible to add custom method like this? Or should I wrap Vector2f into my own class?

Upvotes: 3

Views: 570

Answers (3)

freenatec
freenatec

Reputation: 571

Both the vanilla development package and the full SDK downloads of SFML contain the vector class declaration/definition files. Look in ../include/SFML/System/ (in either package) for the files Vector2.hpp and Vector2.inl. One way to add your rotate function could be:

Add the rotate method to the class declaration in Vector2.hpp:

...stuff...

template <typename T>
class Vector2
{
 public :

 ....

 void Rotate(T angle);

 ....

 };

And then define the method in vector2.inl (to follow convention):

 template <typename T>
 void Vector2<T>::Rotate(T angle) {
      ...your implementation here...
 }

An alternative to modifying the Vector2 class would be to make use of the extended vector math functions in the quite nifty, SFML-based Thor library, which includes a Rotate function. The minimal (un-altered) files you need from the Thor SDK to make use of its 2D vector functions are:

  1. ../include/Thor/Vectors/VectorAlgebra2d.hpp
  2. ../include/Thor/Detail/VectorAlgebra2D.inl
  3. ../include/Thor/Math/Trigonometry.hpp
  4. ../src/Trigonometry.cpp

With those files in your project directory, you could rotate a sf::Vector by doing something like this:

 #include <iostream>
 #include <SFML/Graphics.hpp>
 #include "VectorAlgebra2D.hpp"

....

sf::Vector2f rotate_THIS(10.0f,10.0f);
thor::Rotate(rotate_THIS, 180.0f); //pass by reference

std::cout << "(" << rotate_THIS.x << ", " << rotate_THIS.y << ")" << std::endl;

sf::Vector2f rotated = thor::RotatedVector(rotate_THIS, 180.0f); //returns object

std::cout << "(" << rotated .x << ", " << rotated .y << ")" << std::endl;

....

Which outputs (predictably):

(-10,-10)
(10,10)

I just went through the same process of needing to modify the SFML vector class template for a project, and after poking around the source and adding in a few functions (Length and Dot Product), I stumbled on the Thor library, which has served me pretty well so far.

Upvotes: 1

Chad
Chad

Reputation: 19619

The compiler is complaining because you are trying to implement a function that was not declared in the class declaration. If you have access to the class declaration then you can add this function to the declaration, then define the function as you are trying to do now.

Upvotes: 0

yan
yan

Reputation: 20992

What you're thinking of is called "monkey patching" or categories and is not available in C++. You can derive from that class to add that functionality or create a friend function to implement what you're trying to do. (Functions defined as friend inside a class definition have access to an object's internal state)

Upvotes: 0

Related Questions