lucaay
lucaay

Reputation: 25

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

I have this class and I'm trying to overload the input operator but i can't get it to work. Ex2 outputs exactly what it is supposed to output but ex13 give me segmentation fault after entering 2 values. I'm not allowed to change the type of variables from "private:" access specifier

As input I need to write this(or similar):

14
Dell
150

These inputs are for(translation from romanian to english):

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

Imprimanta = Printer

And here is the code:

#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;
}```

Upvotes: 0

Views: 69

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122595

Disclaimer: The code in the quesiton reads like it was written to demonstrate how broken code can be while it still compiles. If the class definition was given to OP with the request to add an operator<<, there is no way to solve the exercise in any meaningful way but to throw away the code and start from scratch. Anyhow, this is adressing the issue in the operator<< only and ignores the uncountable other issues...

You probably added this to silence an error due to modifying a const:

int* ptr;
ptr = (int*)(&e.serialNumber);
in >> *ptr;

However, all you achieved is to silence the error. The issue is still present. Actually thats what c-style casts do most of the time: They hide a problem without fixing it. You shall not modify an object that is const. If you do, you invoke undefined behavior.

If you want serialNumber to be not modifiable, make it private and provide no mutator methods. Actually thats already the case, so you can simply remove the const and read the value "normal":

in >> e.serialNumber;

Upvotes: 1

Related Questions