Hello guys, I am trying to implement a State Design pattern, but I am stuck with it since I can't get my code compiled, and can't find solution to it
Error happens in this part of the code, while trying to call method setState
Code:
void StartTransmitted::nextState(I2CStateContext* currentContext)
{
cout << "Start Transmitted is running .." << endl;
this->sendSignal();
// this is where error occours
currentContext->setState(new AdrWTransACKRec());
}
StartTransmitted.h
Code:
#pragma once
#ifndef _START_TRANSMITTED_H_
#define _START_TRANSMITTED_H_
#include "I2CState.h"
class StartTransmitted : public I2CState
{
public:
void nextState(I2CStateContext* currentContext);
private:
void sendSignal(void);
};
#endif
StartTransmitted.cpp
Code:
#include "StartTransmitted.h"
#include "AdrWTransACKRec.h"
void StartTransmitted::nextState(I2CStateContext* currentContext)
{
cout << "Start Transmitted is running .." << endl;
this->sendSignal();
// This is line where error happens
currentContext->setState(new AdrWTransACKRec());
}
void StartTransmitted::sendSignal(void)
{
cout << "Sending start signal" << endl;
}
I2CState.h
Code:
#pragma once
#ifndef _I2C_STATE_H_
#define _I2C_STATE_H_
#include <iostream>
class I2CStateContext;
using namespace std;
class I2CState
{
public:
virtual void nextState(I2CStateContext* currentContext);
public:
virtual void sendSignal(void);
};
#endif
I2CStateContext.h
Code:
#include "I2CStateContext.h"
I2CStateContext::I2CStateContext()
{
//this->currentState = new State();
}
void I2CStateContext::setState(I2CState* newState)
{
this->currentState = newState;
}
void I2CStateContext::nextState()
{
currentState->nextState(this);
}
I2CStateContext.cpp
Code:
#pragma once
#ifndef _I2C_CONTEXT_H_
#define _I2C_CONTEXT_H_
#include "I2CState.h"
class I2CStateContext
{
private:
I2CState* currentState;
public:
I2CStateContext();
void setState(I2CState* newState);
void nextState();
};
#endif