Zoe_Cpp 2021-09-08 08: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 14:39
    关注

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

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

报告相同问题?

问题事件

  • 系统已结题 9月15日
  • 已采纳回答 9月8日
  • 创建了问题 9月8日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部