C++ 中的运算符重载

operator -overloading in C++

我有一个构造运算符重载的作业 bitwise.I 尝试构造 operator<< 以将整数左移,但是当我声明函数时: friend const int operator<<(const int& , int& ); 编译器提示错误 nomember operator requier class 或枚举类型的参数。。所以我不使用 operator<< 进行运算符重载。这是我的代码:

#pragma once//bitwise.h
#include<iostream>
using namespace std;
class bitwise
{   private: 
    int n;
public:
    bitwise(int n){
        this->n = n;
    }
    friend const int operator<<(const int& , int& );//complier notice error here.
};
const int operator<<(const int& n, int& x){
    int temp=n*pow(2, x);
    return temp;
}

您创建运算符的方式试图重新定义将左移与两个整数一起使用的含义,而不是与您的 class 一起使用。你想要这样的东西:

friend int operator<<(const bitwise& , int);