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