运算符-C++中的重载

operator -overloading in C++

本文关键字:重载 -C++ 运算符      更新时间:2023-10-16

我有一个赋值,用位构造运算符重载。我试图构造运算符<lt左移整数,但当我声明函数为:friend const int运算符<lt;(const int&,int&编译器注意错误nomember运算符需要类或枚举类型的参数。所以我不使用运算符<lt。这是我的代码:

#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;
}

创建运算符的方式是,它试图重新定义用两个整数向左移位的含义,而不是用类。你想要这样的东西:

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