vectors.h:
Код: Выделить всё
#ifndef INCLUDE_HEADERS_VECTORS_H
#define INCLUDE_HEADERS_VECTORS_H
template class Vector2D;
template Vector2D operator+(Scalar a, Vector2D v);
template
class Vector2D {
public:
Vector2D();
Vector2D(Scalar a, Scalar b);
Scalar getX();
Scalar getY();
Vector2D operator+(Scalar a);
friend Vector2D operator+ (Scalar a, Vector2D v);
private:
Scalar x, y;
};
#endif
Код: Выделить всё
#include
template
Vector2D::Vector2D() {
x = 0;
y = 0;
}
template
Vector2D::Vector2D(Scalar a, Scalar b) {
x = a;
y = b;
}
template
Scalar Vector2D::getX() {
return x;
}
template
Scalar Vector2D::getY() {
return y;
}
template
Vector2D Vector2D::operator+(Scalar a) {
return Vector2D(a + x, a + y);
}
template
Vector2D operator+(Scalar a, Vector2D v) {
return Vector2D(a + v.x, a + v.y);
}
template class Vector2D;
template Vector2D operator+(float a, Vector2D v);
Код: Выделить всё
#include
#include
int main(int argc, char* argv[]){
Vector2D a(1.0, 3.0);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78524795/error-when-changing-order-of-operators-with-templates-in-c[/url]
Мобильная версия