予你以晴 2020-07-02 22:02 采纳率: 100%
浏览 286
已采纳

VS2019 C++ 重载‘+’,‘=’后不能正确的把相加后的表达式赋值给新创建的类。

图片说明
这个是重载的+
图片说明
这个是重载的=

图片说明
这个是主函数
图片说明
这里就是报错内容了

直接两个类相等都没问题,为什么进行重载后的+运算会出错呢?。。。

.cpp

#include "bds.h"
#include<iostream>
#include<iomanip>
using namespace std;
bds::bds(){
    p = new char[80];
    x1 = 0, x2 = 0,x3 = 0;
}
bds::~bds() {
    delete[]p;
}
bds::bds(const char* x)
{
    p = new char[80];
    int t = 0;
    for (; x[t] != '\0'; t++)
    {
        p[t] = x[t];
    }
    p[t] = '\0';
    x1 = 0, x2 = 0, x3 = 0;
}
bds::bds(bds& c)
{
    p = new char[80];
    int i = 0;
    for (; c.p[i] != '\0'; i++)
    {
        p[i] = c.p[i];
    }
    p[i] = '\0';
    x1 = c.x1, x2 = c.x2, x3 = c.x3;
};
ostream& operator<<(ostream& out, bds& c)
{
    out << c.p;
    return out;
};//重载输出字符
istream& operator>>(istream& in, bds& c)
{
    in >> c.p;
    return in;
}
bds operator+(bds& s1, bds& s2)
{
    bds s3;
    s1.shu(), s2.shu();
    s3.x1 = s1.x1 + s2.x1;
    s3.x2 = s1.x2 + s2.x3;
    s3.x3 = s1.x3 + s2.x3;
    if (s3.x1!=0)
    {
        if (s3.x2!=0)
        {
            if (s3.x3!=0)
            {
                bds s("(0x+0x*x+0x*x*x)");
                char c1[2],  c2[2], c3[2];
                _itoa_s(s3.x1, c1, 10);
                _itoa_s(s3.x2, c2, 10);
                _itoa_s(s3.x3, c3, 10);
                s3 = s;
                s3.p[1] = c1[0];
                s3.p[4] = c2[0];
                s3.p[9] = c3[0];

            }
        }
    }
    return s3;
}
void bds::operator=(bds& c)
{
    int i = 0;
    for (; c.p[i] != '\0'; i++)
    {
        p[i] = c.p[i];
    }
    p[i] = '\0';
    x1 = c.x1, x2 = c.x2, x3 = c.x3;
}
void bds::shu()
{
    for (int i = 0; i<strlen(p); i++)
    {
        int k = 0;
        if (p[i] == 'x')
        {
            int j = 0;
            for (; p[i + j] != '+'; j++)
            {

                if (p[i + j] == '\0')
                {
                    if (p[i + j - 1] == ')')
                    {
                        j = j - 1;
                    }
                    break;
                }
            }
            k = j;
            switch (j)
            {
                char str[100];
            case 1: {str[0] = p[i - 1];
                x1 = atoi(str);
                if (p[i - 1] == '+')
                {
                    x1 = 1;
                }
            }
                  break;
            case 3: {str[0] = p[i - 1];
                x2 = atoi(str);
                if (p[i - 1] == '+')
                {
                    x2 = 1;
                }
            }
                  break;
            case 5: {str[0] = p[i - 1];
                x3 = atoi(str);
                if (p[i - 1] == '+')
                {
                    x3 = 1;
                }
            }
            default:
                break;
            }
        }
        i += k;
    }
}
;

.h

#pragma once
#include<iostream>
#include<iomanip>
using namespace std;
class bds
{
private:
    int x1,x2,x3;
    char* p;
public:
    bds();
    ~bds();
    bds(const char* x);
    bds(bds& c);
    friend ostream& operator<<(ostream& out, bds& c);
    friend istream& operator>>(istream& in, bds& c);
    friend bds operator+(bds&s1,bds&s2);
    void operator=(bds& c);
    void show()
    {
        cout << x1 << "," << x2 << "," << x3;
    }
    void shu();
};

主程序

#include<iostream>
#include<iomanip>
#include"bds.h"
using namespace std;
int main()
{
    bds t("(4x*x*x+3x*x+x)");
    bds w("(2x+2x*x+2x*x*x)");
    t.shu();
    w.shu();
    bds q;
    q = t+w;
    q = t;
    q.shu();
    //char s1[100]{ "(2x*x+1)" };
    //cout << t << endl;
    /*t.show();*/
    q.show();
    cout << q;
    return 0;
}
  • 写回答

1条回答 默认 最新

  • wlj1234 2020-07-04 15:57
    关注

    bds operator+(bds& s1, bds& s2) 改为bds operator+(const bds& s1, const bds& s2)
    void operator=(bds& c) 改为void operator=(const bds& c);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名