Class 继承和运算符重载
Class inheritance and operator overloading
我正在通过 SFML 上的一个小项目学习 C++,我想扩展 sf::Vector2<T>
class,它表示一个二维数学向量。这个class的声明是available here。特别是,我想向 class 添加一些方法,用于范数计算、旋转等
这是我目前所拥有的:
#include <cmath>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Transform.hpp>
#include <ostream>
namespace dw::math {
template <typename T>
class Vector2 : public sf::Vector2<T> {
public:
// Don't really know what does that exactly means, explainations are welcome !
using sf::Vector2<T>::Vector2;
template <typename U>
explicit Vector2(const U &vector) : sf::Vector2<T>(vector) {}
// This ctor is for implicit conversion from base, don't know if it's really useful ?
Vector2(sf::Vector2<T> &vector) : sf::Vector2<T>(vector) {}
// Some methods here ...
friend std::ostream & operator<<(std::ostream &os, const math::Vector2<T>& right) {
os << '{' << right.x << ", " <<right.y << '}';
return os;
}
};
稍后在代码中,我声明了这样一个结构:
struct Derivative {
dw::math::Vector2f dPos;
dw::math::Vector2f dVel;
Derivative() = default;
Derivative(const dw::math::Vector2f &dPos, const dw::math::Vector2f &dVel) : dPos(dPos), dVel(dVel) {}
Derivative operator+(const Derivative &rhs) const {
return Derivative(
dPos + rhs.dPos,
dVel + rhs.dVel
);
}
}
Derivative
的 operator+
重载的 return 部分无法编译:
/home/palra/Documents/Projets/dw/src/physics/World.cpp: In member function ‘Derivative Derivative::operator+(const Derivative&) const’:
/home/palra/Documents/Projets/dw/src/physics/World.cpp:24:18: error: invalid user-defined conversion from ‘sf::Vector2<float>’ to ‘const Vector2f& {aka const dw::math::Vector2<float>&}’ [-fpermissive]
dPos + rhs.dPos,
~~~~~^~~~~~~~~~
In file included from /home/palra/Documents/Projets/dw/src/physics/Body.h:8:0,
from /home/palra/Documents/Projets/dw/src/physics/World.h:10,
from /home/palra/Documents/Projets/dw/src/physics/World.cpp:5:
/home/palra/Documents/Projets/dw/src/physics/../math/Vector2.h:30:5: note: candidate is: dw::math::Vector2<T, <template-parameter-1-2> >::Vector2(sf::Vector2<T>&) [with T = float; <template-parameter-1-2> = void] <near match>
Vector2(sf::Vector2<T> &vector) : sf::Vector2<T>(vector) {}
我不明白为什么这不起作用。表达式 dPos + rhs.dPos
与 sf::Vector2<float> operator +(const sf::Vector2<float>& left, const sf::Vector2<float>& right)
的调用兼容,因为 dw::math::Vector2<float>
是继承的 sf::Vector2<float>
。然后这个表达式应该产生一个 sf::Vector2<float>
,这要归功于我猜的非显式构造函数,它可以分配给 dw::math::Vector2f
。我哪里错了?我该如何让它发挥作用?
sf::Vector<T>
并未设计为派生 class,因此更好的方法是编写扩展 sf::Vector<T>
功能的免费函数,这可以无缝地用于运营商。不幸的是,对于正常的函数调用,C++ 还不支持像 C# 那样的扩展。
我正在通过 SFML 上的一个小项目学习 C++,我想扩展 sf::Vector2<T>
class,它表示一个二维数学向量。这个class的声明是available here。特别是,我想向 class 添加一些方法,用于范数计算、旋转等
这是我目前所拥有的:
#include <cmath>
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Transform.hpp>
#include <ostream>
namespace dw::math {
template <typename T>
class Vector2 : public sf::Vector2<T> {
public:
// Don't really know what does that exactly means, explainations are welcome !
using sf::Vector2<T>::Vector2;
template <typename U>
explicit Vector2(const U &vector) : sf::Vector2<T>(vector) {}
// This ctor is for implicit conversion from base, don't know if it's really useful ?
Vector2(sf::Vector2<T> &vector) : sf::Vector2<T>(vector) {}
// Some methods here ...
friend std::ostream & operator<<(std::ostream &os, const math::Vector2<T>& right) {
os << '{' << right.x << ", " <<right.y << '}';
return os;
}
};
稍后在代码中,我声明了这样一个结构:
struct Derivative {
dw::math::Vector2f dPos;
dw::math::Vector2f dVel;
Derivative() = default;
Derivative(const dw::math::Vector2f &dPos, const dw::math::Vector2f &dVel) : dPos(dPos), dVel(dVel) {}
Derivative operator+(const Derivative &rhs) const {
return Derivative(
dPos + rhs.dPos,
dVel + rhs.dVel
);
}
}
Derivative
的 operator+
重载的 return 部分无法编译:
/home/palra/Documents/Projets/dw/src/physics/World.cpp: In member function ‘Derivative Derivative::operator+(const Derivative&) const’:
/home/palra/Documents/Projets/dw/src/physics/World.cpp:24:18: error: invalid user-defined conversion from ‘sf::Vector2<float>’ to ‘const Vector2f& {aka const dw::math::Vector2<float>&}’ [-fpermissive]
dPos + rhs.dPos,
~~~~~^~~~~~~~~~
In file included from /home/palra/Documents/Projets/dw/src/physics/Body.h:8:0,
from /home/palra/Documents/Projets/dw/src/physics/World.h:10,
from /home/palra/Documents/Projets/dw/src/physics/World.cpp:5:
/home/palra/Documents/Projets/dw/src/physics/../math/Vector2.h:30:5: note: candidate is: dw::math::Vector2<T, <template-parameter-1-2> >::Vector2(sf::Vector2<T>&) [with T = float; <template-parameter-1-2> = void] <near match>
Vector2(sf::Vector2<T> &vector) : sf::Vector2<T>(vector) {}
我不明白为什么这不起作用。表达式 dPos + rhs.dPos
与 sf::Vector2<float> operator +(const sf::Vector2<float>& left, const sf::Vector2<float>& right)
的调用兼容,因为 dw::math::Vector2<float>
是继承的 sf::Vector2<float>
。然后这个表达式应该产生一个 sf::Vector2<float>
,这要归功于我猜的非显式构造函数,它可以分配给 dw::math::Vector2f
。我哪里错了?我该如何让它发挥作用?
sf::Vector<T>
并未设计为派生 class,因此更好的方法是编写扩展 sf::Vector<T>
功能的免费函数,这可以无缝地用于运营商。不幸的是,对于正常的函数调用,C++ 还不支持像 C# 那样的扩展。