Adding a Method to the Library

I recently wrote a simple game in C ++ using SFML. Here is my question:

There is a template class Vector2<T>in the SFML library (in particular, I would use Vector2f). Unfortunately, he has no way to turn himself, so I came up with the idea to write it. But, as I wrote:

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

the compiler says that I cannot do things like this:

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

Can I add my own method? Or do I need to port Vector2f to my own class?

+3
source share
3 answers

, , " " ++. , friend, , . (, friend , )

0

, SDK SFML / . ../include/SFML/System/( ) Vector2.hpp Vector2.inl. :

rotate Vector2.hpp:

...stuff...

template <typename T>
class Vector2
{
 public :

 ....

 void Rotate(T angle);

 ....

 };

vector2.inl( ):

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

Vector2 , SFML Thor, . ( ) , Thor SDK, 2D- :

  • ..//Thor/Vectors/VectorAlgebra2d.hpp
  • ..//Thor/Detail/VectorAlgebra2D.inl
  • ..//Thor//Trigonometry.hpp
  • ../SRC/Trigonometry.cpp

sf:: Vector, - :

 #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;

....

():

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

, SFML , , (Length Dot Product), Thor, .

+1

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

0
source

All Articles