首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >算子-overloading在C++中的应用

算子-overloading在C++中的应用
EN

Stack Overflow用户
提问于 2016-04-10 14:34:42
回答 1查看 106关注 0票数 0

我有一个赋值,用but构造操作符重载,我尝试构造operator<<以向左移动整数,但是当我声明函数时是:friend const int operator<<(const int&,int& );编者注意到错误所以我不让操作员超载operator<<。这是我的密码:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-10 14:40:09

在创建操作符的过程中,它试图重新定义使用shift(左移)和两个整数,而不是类的含义。你想要这样的东西:

代码语言:javascript
复制
friend int operator<<(const bitwise& , int);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36531069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档