幡然醒悟0222 2019-06-04 18:24 采纳率: 50%
浏览 356

数组元素ch[j]=='('报错,我是想作比较不是赋值,编译器报错->'=' : left operand must be l-value 。

数组元素ch[j]=='('报错,我是想作比较不是赋值,编译器报错->'=' : left operand must be l-value (请帮忙看看哪里有问题,附上源码和报错)。

源码:

#include <iostream>
#include <string.h>
// #include <stdio.h>
// #include <cmath>
#include <string>
using namespace std;

class Rational
{
protected:
    string str;
    int integer;  //整数部分
    int point[2]; //小数部分,又分为非循环与循环部分

public:
    Rational();
    Rational(string st);
    void countInteger();         //取整数部分
    void countPoint();           //取小数部分,计算作为小数部分时分母和分子
    int countSimplify();         //当只有小数部分时,化简
    void countAdd();             //,化简后整合两部分,相加
    void integerTransfDecimal(); //show
    friend istream &operator>>(istream &input, Rational &Ra);
};

Rational::Rational()
{
    this->str = "0";
    this->integer = 0;
    this->point[0] = 0;
    this->point[1] = 0;
}

Rational::Rational(string st)
{
    this->str = st;
    this->integer = 0;
    this->point[0] = 0;
    this->point[1] = 0;
}
//取整数部分
void Rational::countInteger()
{
    char ch[15];
    for (int i = 0; i < str.length(); i++)
    {
        if (ch[i] != '.')
        {
            ch[i] = str[i];
            this->point[0]++; //临时计数
        }
        else
            break;
    }
}
//取小数部分,计算作为小数部分时分母和分子
void Rational::countPoint()
{
    char ch[15];
    int te = this->point[0];
    for (int i = te + 1; i < str.length(); i++)
    {
        ch[i] = str[i];
    }
    int px = 0, py = 0, pm = 0;
    //计算位数
    for (int j = 0; j < str.size(); j++)
    {
        if (ch[j] != '(' &&pm = 0)
        {
            this->point[0] *= 10;
            this->point[0] += ch[j] - '0'; //得到非循环数值
            px++;                          //记非循环位数
        }
        if (ch[j] == '(' &&pm = 0)
        {
            pm = 1;
            j++; //记循环内位数
        }
        if (ch[j] != ')' &&pm = 1)
        {
            this->point[1] *= 10;
            this->point[1] += ch[j] - '0';
            py++;
        }
    }

    //判断类型,计算分母分子最终值的大小
    if (pm == 1) //如果为非循环小数(例:0.5(0))
    {
        int pw = px - 1;
        int pn = this->point[0];
        this->point[1] = 9;
        while (pw--)
        {
            this->point[0] *= 10;
        }
        pw = px;
        while (pw--)
        {
            this->point[1] *= 10;
        }
        this->point[0] -= pn;
    }
    if (pm == 0 && px == 1) //如果为纯循环小数(例:0.(3))
    {
        int pw = py;
        int pn = this->point[0];
        this->point[1] = 9;
        while (pw--)
        {
            this->point[0] *= 10;
            this->point[1] *= 10;
            this->point[1] += 9;
        }
        this->point[0] -= pn;
    }
    if (pm == 0 && px == 0) //如果为非纯循环小数(例:0.52(3))
    {
        int pw = px;
        int pu = py;
        int pn = this->point[0];
        this->point[1] = 9;
        while (pu--)
        {
            this->point[0] *= 10;
        }
        while (pw--)
        {
            this->point[1] *= 10;
        }
        this->point[0] -= pn;
    }
}

//当只有小数部分时,化简
int Rational::countSimplify()
{
    int te = 0;
    for (int i = 1; i <= this->point[1]; i++)
    {
        if (this->point[0] / i == this->point[1] / i)
        {
            te = i; //i最小值应该为1
        }
    }
    return te;
}

//,化简后整合两部分,相加
void Rational::countAdd()
{
    this->integer *= this->point[1];
    this->point[0] *= this->integer;
}
//show
void Rational::integerTransfDecimal()
{
    this->countInteger();
    this->countPoint();
    this->countSimplify();
    this->countAdd();
    cout << "\ninteger transform decimal is :" << endl;
}

istream &operator>>(istream &input, Rational &Ra)
{
    cout << "Please input a point(小数)" << endl;
    input >> Ra.str;
    return input;
}

int main()
{
    Rational ra1;
    cin >> ra1;
    ra1.integerTransfDecimal();
    return 0;

编译错误提示:

: error C2106: '=' : left operand must be l-value
: error C2106: '=' : left operand must be l-value
: error C2106: '=' : left operand must be l-value
 : error C2248: 'str' : cannot access protected member declared in class 'Rational'
        : see declaration of 'str'
 : error C2593: 'operator >>' is ambiguous
执行 cl.exe 时出错.

测试八.exe - 1 error(s), 0 warning(s)

谢谢谢谢!

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-06-04 19:24
    关注

    if (ch[j] != '(' &&pm = 0)
    ->
    if (ch[j] != '(' &&pm == 0)

    istream &operator>>(istream &input, Rational &Ra)
    这个要么申明成友元函数,要么把str定义为public

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的