Zoe_Cpp 2021-09-08 16:33 采纳率: 100%
浏览 39
已结题

c++ 输出<<重载,实现对象输出发生问题

自己利用C++类实现python的整型对象
但是一直报错
二元“<<”: 没有找到接受“PyInt::PyIntCell”类型的右操作数的运算符(或没有可接受的转换)
不理解这个报错什么意思

img

PyInt.h

#pragma once
#include <string>

class PyInt {
private:

    //数组元素单位为long long
    typedef long long PyIntCell;

    //指向长整型数组的指针
    PyIntCell* pPyIntArr;

    //数组长度
    int PyIntArrLength = 0;

    //是否为负数
    bool isNegative = 0;
public:
    //构造函数
    PyInt();

    //析构函数
    ~PyInt();

    //将字符串赋值给PyInt类型
    const PyInt& operator = (const std::string& _cpp_string);

    //将整数赋值给PyInt类型
    const PyInt& operator = (const int& _cpp_ll_int);

    //将PyInt类型赋值给PyInt类型
    const PyInt& operator = (const PyInt& _py_int);

    //将PyInt类型输出
    friend std::ostream& operator << (std::ostream& out,const PyInt& _py_int);

    //将PyInt类型输入
    friend std::istream& operator >> (const std::istream& in, PyInt& _py_int);
};

PyInt.cpp

#include "PyInt.h"

PyInt::PyInt() {
    pPyIntArr = new PyIntCell[1];
    pPyIntArr[0] = 0;
}

PyInt::~PyInt() {
    if (pPyIntArr == NULL)
        return;
    delete[] pPyIntArr;
}

const PyInt& PyInt::operator=(const PyInt& _py_int) {
    if(pPyIntArr!=nullptr)
        delete[] pPyIntArr;
    this->pPyIntArr = new PyIntCell[PyIntArrLength];
    for (int Floop = 0; Floop < PyIntArrLength; Floop++) {
        this->pPyIntArr[Floop] = _py_int.pPyIntArr[Floop];
    }
    this->PyIntArrLength = _py_int.PyIntArrLength;
    this->isNegative = _py_int.isNegative;
    return *this;
}

const PyInt& PyInt::operator=(const int& _cpp_int) {
    if(pPyIntArr!=nullptr)
        delete[] pPyIntArr;
    this->pPyIntArr = new PyIntCell[1];
    this->pPyIntArr[0] = _cpp_int;
    this->PyIntArrLength = 1;
    if (_cpp_int < 0)
        this->isNegative = false;
    else
        this->isNegative = true;
    return *this;
}

const PyInt& PyInt::operator=(const std::string& _cpp_string) {
    if (pPyIntArr != nullptr)
        delete[] pPyIntArr;
    int iStringLength = _cpp_string.length();
    int Rloop = 0;
    int PyArrPos = 0;
    int StrStartLoop = iStringLength - 1;
    int StrEndLoop;
    this->PyIntArrLength = iStringLength / 18 + 1;
    this->pPyIntArr = new PyIntCell[PyIntArrLength];
    
    if (_cpp_string[0] == '-') {
        this->isNegative = true;
        StrEndLoop = 1;
    }
    else {
        this->isNegative = false;
        StrEndLoop = 0;
    }
    for (int FLoop = 0; FLoop < PyIntArrLength; FLoop++) {
        this->pPyIntArr[FLoop] = 0;
    }
    for (; StrStartLoop >= StrEndLoop; StrStartLoop--) {
        this->pPyIntArr[PyArrPos] += (_cpp_string[StrStartLoop] - 48) * pow(10, Rloop);
        Rloop == 18 ? Rloop = 0, PyArrPos++ : Rloop++;
    }
    return *this;
}

std::ostream& operator << (std::ostream& out,const PyInt& _py_int) {
    for (int Floop = _py_int.PyIntArrLength - 1; Floop >= 0; Floop--) {
        out << _py_int.pPyIntArr[Floop];//*****这里报错******
    }
    return out;
}

  • 写回答

2条回答 默认 最新

  • Zoe_Cpp 2021-09-08 22:39
    关注

    解决了,没有导入iostream,,,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月16日
  • 已采纳回答 9月8日
  • 创建了问题 9月8日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘