Well, the = operator uses the != operator which uses the == operator, so indirectly, setCoord function uses the == operator. And I ran my code through a debugger, and it told me that it received a segmentation fault at this line of code return(this->X==p.get_x() && this->Y==p.get_y());.
I now get a segmentation fault at that line of code.
One more edit:
I tried just taking out the if statement in my = operator definition to see if it would work, and now I get a segmentation fault at line 4 of:
Also, doing if(*this!=p){ in your assignment operator is completely pointless. It's more work to check to see if the two objects are equivilent than it would be to just unconditionally assign them.
Also, I was under the impression that it is a good coding practice for when you are implementing a method that uses member variables of a class to precede the member variable with this->. Is this not the case? If not, when and when should I not do it?
Okay that's your problem. You're creating a bunch of pointers, but those pointers don't point to anything. Therefore you're trying to assign objects that don't exist.
Get rid of the pointer nonsense. You don't need it here.
Ok that makes total sense. I didn't have pointers initially, but the only reason why I changed it to pointers was because, ultimately I have several sub classes that I want to use:
class Player : public cell{
public:
Player();
~Player();
virtual cell_type getType() const;
virtualvoid print(std::ostream &) const;
};
class Beast : public cell{
public:
Beast();
~Beast();
virtual cell_type getType() const;
virtualvoid print(std::ostream &) const;
};
class Movable : public cell{
public:
Movable();
~Movable();
virtual cell_type getType() const;
virtualvoid print(std::ostream &) const;
};
class Immovable : public cell{
public:
Immovable();
~Immovable();
virtual cell_type getType() const;
virtualvoid print(std::ostream &) const;
};
class Empty : public cell{
public:
Empty();
~Empty();
virtual cell_type getType() const;
virtualvoid print(std::ostream &) const;
};
I wanted to create a 2D vector of generic cells, and then based on what the user inputted, change the cells to a more specific kind of cell. I thought I could only typecast a base class into a derived class through the use of pointers, no?