최근에 연산자 오버로드를 시도했으며 연산자 오버로딩에 대한이 stackoverflow 페이지 (http://stackoverflow.com/questions/4421706/operator-overloading)를 살펴 보았습니다. 나는 * 연산자를 오버로드하고 여기에오버로드 된 연산자 << 연산자 사용시 오류 *
std::cout << a*b;
과 같은 코드를 실행하면 컴파일 타임 오류 error: invalid operands to binary expression ('basic_ostream<char, std::char_traits<char> >' and 'Vector2')
을
Vector2 a(2, 3);
Vector2 b(5, 8);
Vector2 c = a*b;
과 같은 코드를 실행하지만, 얻을 수
는 Vector2입니다. cpp
#include "Vector2.h"
Vector2::Vector2(const float x, const float y) {
this->x = x;
this->y = y;
}
Vector2 &Vector2::operator*=(const Vector2 &rhs) {
this->x *= rhs.x;
this->y *= rhs.y;
return *this;
}
std::ostream &operator<< (std::ostream &out, Vector2 &vector) {
return out << "(" << vector.x << ", " << vector.y << ")";
}
여기에 내가 당장은 확실하지 않다 Vector2.h
#include <iostream>
class Vector2 {
public:
float x;
float y;
Vector2(const float x, const float y);
Vector2 &operator*=(const Vector2 &rhs);
};
inline Vector2 operator*(Vector2 lhs, const Vector2 &rhs) {
lhs *= rhs;
return lhs;
}
std::ostream &operator<<(std::ostream &out, Vector2 &vector);
입니다.
은 괄호를 사용하여 검사 한? – betabandido
예, 제가 시도한 첫 번째 시도 였지만 작동하지 않았습니다. – rabbidrabbit
다음과 같은 이유가 있습니다. http://en.cppreference.com/w/cpp/language/operator_precedence – chris