输入运算符重载无法正常工作//用户输入后出现分段错误

Input operator overloading not working properly // segmentation fault after input from user

我有这个 class,我正在尝试使输入运算符超载,但我无法让它工作。 Ex2 准确输出它应该输出的内容,但 ex13 在输入 2 个值后给我分段错误。 我不允许从“私有:”访问说明符

更改变量类型

作为输入,我需要写这个(或类似的):

14
Dell
150

这些输入用于(从罗马尼亚语翻译成英语):

serial number  // serial number
producator  //manufacture
nrPaginiPrintate // number of printed pages

Imprimanta = Printer

这是代码:

#include <string>
using namespace std;
class Imprimanta
{
private:
    const int serialNumber;
    char* producator;
    int nrPaginiPrintate;
    static string tipImprimanta;
public:
    Imprimanta() :serialNumber(1)
    {
        this->producator = (char*)"";
        this->nrPaginiPrintate = 0;
    }

    Imprimanta(int serialNumber,  const char* producator, int nrPaginiPrintate) :serialNumber(serialNumber)
    {
        string* str = new string();
        *str = producator;
        this->producator = (char*)(str->c_str());
        this->nrPaginiPrintate = nrPaginiPrintate;
    }

    ~Imprimanta(){}

    Imprimanta(const Imprimanta& i) :serialNumber(i.serialNumber) {
        string* str = new string();
        *str = i.producator;
        this->producator = (char*)(str->c_str());
        this->nrPaginiPrintate = i.nrPaginiPrintate;
        this->tipImprimanta = i.tipImprimanta;
    }

    Imprimanta operator=(Imprimanta i)  {
        string* str = new string();
        *str = i.producator;
        this->producator = (char*)(str->c_str());
        this->nrPaginiPrintate = i.nrPaginiPrintate;
        this->tipImprimanta = i.tipImprimanta;
        int* ptr;
        ptr = (int*)(&serialNumber);
        *ptr = i.serialNumber;
        return *this;
    }

    bool operator<(Imprimanta t)
    {
        if (nrPaginiPrintate < t.nrPaginiPrintate) {
            return true;
        }
        else {
            return false;
        }
    }

    int get_nrPaginiPrintate() {
        return nrPaginiPrintate;
    }
    void set_nrPaginiPrintate(int pagini_print) {
        this->nrPaginiPrintate = pagini_print;
    }

    static string getTipImprimanta()
    {
        return tipImprimanta;
    }

    static void setTipImprimanta(string tipImprimanta_a)
    {
        tipImprimanta = tipImprimanta_a;
    }

    int get_serialNumber() {
        return serialNumber;
    }
    friend ostream& operator<<(ostream& out, const Imprimanta& e);
    friend istream& operator>>(istream& in, Imprimanta& e);
};
string Imprimanta::tipImprimanta = "Cerneala";

ostream& operator<<(ostream& out, const Imprimanta& e)
{
    out << "Serial Number: " << e.serialNumber << '\n';
    out << "Producator: " << e.producator << '\n';
    out << "Numar pagini printate: " << e.nrPaginiPrintate << '\n';
    return out;
}

istream& operator>>(istream& in, Imprimanta& e)
{
    int* ptr;
    ptr = (int*)(&e.serialNumber);
    in >> *ptr;
    in >> e.producator;
    in >> e.nrPaginiPrintate;
    return in;
}

int main()
{


    //2
    cout << "-----------------------EX_2-----------------------";
    cout << '\n';
    Imprimanta i2(2, "Canon", 160);
    cout << i2;
    cout << '\n';

    //13
    cout << "-----------------------EX_13-----------------------";
    cout << '\n';
    Imprimanta i11;
    cin >> i11;
    cout << i11;
    cout << '\n';

    return 0;
}```

免责声明:问题中的代码读起来就像是为了演示代码在编译时可能有多么损坏而编写的。如果 class 定义被提供给 OP 并请求添加 operator<<,则没有办法以任何有意义的方式解决练习,只能丢弃代码并从头开始。无论如何,这只是解决 operator<< 中的问题,而忽略了无数其他问题...

您添加这个可能是为了消除由于修改 const:

而导致的错误
int* ptr;
ptr = (int*)(&e.serialNumber);
in >> *ptr;

但是,您所做的只是消除错误。问题仍然存在。实际上这就是大多数时候 c 风格的转换所做的事情:他们隐藏了一个问题而不是修复它。您不得修改 const 的对象。如果这样做,您将调用未定义的行为。

如果您希望 serialNumber 不可修改,请将其设置为 private 并且不提供修改器方法。实际上已经是这样了,所以你可以简单地删除 const 并读取值 "normal":

in >> e.serialNumber;