jeudi 3 mars 2016

Using Memento Pattern to return an objects state

thanks for reading this question. I'm trying to implement the Memento design pattern just so I can see how things work. First off I followed an online tutorial which saves a state in form of a string such as "State 1" and then after creating a new state (also a string) "State 2", I was able to then restore the previous state "State 1". However, I would like to store and restore more complex object states such as a Shape object which I could call getters and setters on. Here's what I have so far...

class Shape
{
private:
    string shape;
public:
Shape();
Shape(string s)
{
    this->shape = s;
}
void setShape(string s)
{
    this->shape = s;
}
string getShape()
{
    return shape;
}
};


 class Memento
{
private:
    Shape state;
public:
Memento(Shape shape)
{
    this->state = shape;
}
Shape getState()
{
    return state;
}
};

class Originator
{
private:
    Shape state;
public:
void setStateShape(string s)
{
    this->state.setShape(s);
}
string getStateShape()
{
    return state.getShape();
}
Memento saveStateToMemento()
{
    return Memento(state);
}
void getStateFromMemento(Memento memento)
{
    state = memento.getState();
}

};

class Caretaker
{
private:
vector<Memento> mementoList;
public:
void add(Memento state)
{
    mementoList.push_back(state);
}
Memento get(int index)
{
    return mementoList[index];
}
};


int main()
{
Originator originator;
Caretaker caretaker;

originator.setStateShape("Square");

caretaker.add(originator.saveStateToMemento());

cout << "Shape is " << originator.getStateShape() << endl;

//originator.getStateFromMemento(caretaker.get(0));

system("pause");
return 0;
}

I was pretty much expecting some sort of error, but I'm thinking this should be kinda straight forward? Here's the error...

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Shape::Shape(void)" (??0Shape@@QAE@XZ) referenced in function "public: __thiscall Memento::Memento(class Shape)" (??0Memento@@QAE@VShape@@@Z) C:\Users\Rhys\Documents\Year 3\Object Oriented Systems\Second Term\MementoTest\main.obj MementoTest

Aucun commentaire:

Enregistrer un commentaire