-2

This might be obvious for some but i really could not get around it

Inside Material I overloaded == operator :

`

class
    Material{
    int id;
    int count;
    double price;
    string name;

    Material() {

    }
    Material(int id) {
        this->id = id;
    }
    Material(int id,int count,double price,string name) {
        this->id = id;
        this->count = count;
        this->name = name;
        this->price = price;
    }
    string getName() {
        return name;
    }
     bool operator==(Material& obj)
    {
        if (this->name == obj.getName())return true;
        else return false;
    }`

And when ever i do smth similar to this : if(obj ==NULL){...}

The program stops and throws an exception.

Exception thrown at 0x0F61D6F0 (ucrtbased.dll) in TradingVendors.exe: 0xC0000005: Access violation reading location 0x00000000.

How can i possibly fix this? thanks

ibrahim
  • 573
  • 1
  • 6
  • 20
  • 5
    [mcve] please. In particular include any constructors if you have them. – StoryTeller - Unslander Monica Mar 18 '18 at 19:28
  • 1
    Checkout this: https://stackoverflow.com/a/2099905/6537157 – akop Mar 18 '18 at 19:36
  • 1
    It's a good idea to implement `operator==` as a non-member function to allow for more encapsulation and implicit conversions on both operands. Also, you should use a const reference. – eesiraed Mar 18 '18 at 19:38
  • You could not check for whether a reference to an object is NULL or not. – Raindrop7 Mar 18 '18 at 19:46
  • @Raindrop7 What i am really doing is passing the name of one object to a Material return type function which is inside a linkedlist .In that function i search whether any element has the same name and if not i return a null. since the above example gives the exact same error and also because my codes are messy i did not include the linkedlist codes in the question . – ibrahim Mar 18 '18 at 19:55

1 Answers1

-2

like @Fei Xiang 's comment make it as a non-member function. here you can define the ==operator as a friend function like below. Then you don't need to use getName() anymore.

class Material
{
private:
    int id;
    int count;
    double price;
    std::string name;
public:
    Material()
        :id(0), count(0),price(0.00), name("unknown")
        {}
    Material(const int& id)
        :id(id)
        {}
    Material(const int& id, const int& count, const double& price,
             const std::string& name)
    {   // use initializer list instead
        this->id = id;
        this->count = count;
        this->name = name;
        this->price = price;
    }
    //const std::string& getName()const { return name;    }

    friend bool operator== (const Material& obj1, const Material& obj2);
};
bool operator== (const Material& obj1, const Material& obj2)
{
    return (obj1.name == obj2.name)? true: false;
}

Comments:

  1. use constructors and member initializer lists(like above).
  2. usage of different names for both parameters and class member variables is good practice.
  • 1
    This doesn't answer the question , it just refactors the code slightly. The `getName()` was not needed in the original case either – M.M Mar 18 '18 at 22:04