Я ожидал, что + = и << будут работать одинаково, но << как-то не работает. </p>
вот мой код:
#include <iostream>
using namespace std;
struct Pos{
int x;
int y;
void operator+=(Pos vel){
x += vel.x;
y += vel.y;
}
};
struct Obj{
string name;
Pos pos;
void info(){
cout << name << endl;
cout << pos.x << ", " << pos.y << endl;
cout << endl;
}
void operator<<(Pos vel){
pos += vel;
}
void operator+=(Pos vel){
pos += vel;
}
};
int main(){
Pos p{10, 20};
Obj car{"Car", p};
Obj truck{"Big truck", {40, 20}};
car.info();
truck.info();
//doesn't work
car << {0, 10};
//works
car += {5, 10};
//works
car << Pos{0, 10};
//works
car += Pos{5, 10};
car.info();
}
большинство из них работает, но
car << {0, 10};
Показывает:
[Error] expected primary-expression before '{' token
Мне интересно, в чем разница между +=
и <<
и почему будет работать конструктор.
Что мне здесь не хватает?