Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将重载运算符_C++_Operator Overloading - Fatal编程技术网

C++ 将重载运算符

C++ 将重载运算符,c++,operator-overloading,C++,Operator Overloading,我最近尝试了操作符重载,并查看了这个stackoverflow页面http://stackoverflow.com/questions/4421706/operator-overloading 关于运算符重载 我重载了*运算符,可以运行如下代码 Vector2 a(2, 3); Vector2 b(5, 8); Vector2 c = a*b; std::cout << a*b; 但获取编译时错误:二进制表达式“basic_ostream”和“Vector2”的操作数无效 运行代

我最近尝试了操作符重载,并查看了这个stackoverflow页面http://stackoverflow.com/questions/4421706/operator-overloading 关于运算符重载

我重载了*运算符,可以运行如下代码

Vector2 a(2, 3);
Vector2 b(5, 8);
Vector2 c = a*b;
std::cout << a*b;
但获取编译时错误:二进制表达式“basic_ostream”和“Vector2”的操作数无效

运行代码时,例如

Vector2 a(2, 3);
Vector2 b(5, 8);
Vector2 c = a*b;
std::cout << 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 << ")";
}
这是向量2.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);
我不知道接下来该怎么办。

问题是

a*b
返回一个临时值,因此您需要:

std::ostream &operator<<(std::ostream &out, const Vector2 &vector);
//                                            |
//                                      notice const                                                  
作为临时变量,无法绑定到非常量引用。

问题在于

a*b
返回一个临时值,因此您需要:

std::ostream &operator<<(std::ostream &out, const Vector2 &vector);
//                                            |
//                                      notice const                                                  

作为临时变量,无法绑定到非常量引用。

以下操作应有效:

Vector2 c = a*b;
std::cout << c;

以下方面应起作用:

Vector2 c = a*b;
std::cout << c;


你有没有用括号检查过:cout是的,这是我第一次尝试,但没有成功。下面是证明为什么不成功的证据:可能重复的为什么不可以让你用括号检查过:cout是的,这是我第一次尝试,但没有成功。下面是证明为什么不成功:可能重复的为什么不可以成功。我想做的是做std::cout,然后你应该更新操作符签名,像其他人建议的那样接受const参数。是的,这确实有效。我想做的是做std::cout,然后你应该更新操作符签名,像其他人建议的那样接受const参数?他拥有它的方式是完美的。你必须复制对象,所以为什么不在参数列表中做呢?谢谢,这很有意义。@Benjamindley为什么要复制到对象?@LuchiangRigure:你能不复制就编写函数吗?@LuchiangRigure我不明白你怎么写函数而不复制。复印要容易得多。至于你的第二点,为什么?他拥有它的方式是完美的。你必须复制对象,为什么不在参数列表中复制呢?谢谢,这很有道理。@BenjaminLindley为什么要复制对象?@LuchianGrigore:你能不复制就写函数吗?@LuchianGrigore我不明白你怎么能不复制就写函数。复印要容易得多。