Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4040888
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:48:37+00:00 2026-05-20T12:48:37+00:00

I’m trying to implement a State Pattern in C++, but have problems with the

  • 0

I’m trying to implement a State Pattern in C++, but have problems with the circular dependency. I have read other related material here – unfortunately it didn’t help me. I don’t have a lot of experience with C++, so bear with me.
The following code is developed on a Ubuntu 10.10 machine in Eclipse Helios CDT:

ConcreteSystem.h

#ifndef CONCRETESYSTEM_H_
#define CONCRETESYSTEM_H_

class SystemState;

class ConcreteSystem {
public:
    ConcreteSystem();
    void SelfTestFailed();
    void Restart();
private:
    friend class SystemState;
    SystemState *currentState;
    void ChangeState(SystemState *state);
};

#endif /* CONCRETESYSTEM_H_ */

ConcreteSystem.cpp

#include "ConcreteSystem.h"
#include "SystemState.h"

ConcreteSystem::ConcreteSystem() {
    currentState = SelfTest::GetInstance();
}

void ConcreteSystem::SelfTestFailed() {
    currentState->SelfTestFailed(this);
}

void ConcreteSystem::Restart() {
    currentState->Restart(this);
}

void ConcreteSystem::ChangeState(SystemState *state){
    currentState = state;
}

SystemState.h

#ifndef SYSTEMSTATE_H_
#define SYSTEMSTATE_H_

class ConcreteSystem;

class SystemState {
public:
    virtual void Restart(ConcreteSystem *cs);
    virtual void SelfTestFailed(ConcreteSystem *cs);
protected:
    virtual void ChangeState(ConcreteSystem *cs, SystemState *state);
};

#endif /* SYSTEMSTATE_H_ */

SystemState.cpp

#include "SystemState.h"
#include "ConcreteSystem.h"

void SystemState::Restart(ConcreteSystem *cs) {

}

void SystemState::SelfTestFailed(ConcreteSystem *cs) {

}


void SystemState::ChangeState(ConcreteSystem *cs, SystemState *state) {
    cs->ChangeState(state);
}

SelfTest.h

#ifndef SELFTEST_H_
#define SELFTEST_H_

#include "SystemState.h"


class SelfTest : public SystemState {
public:
    SelfTest();
    void SelfTestFailed(ConcreteSystem* cs);
    static SystemState* GetInstance();
private:
    static SystemState* instance;
};

#endif /* SELFTEST_H_ */

SelfTest.cpp

#include "SelfTest.h"
#include "Failure.h"

SystemState* SelfTest::instance = 0;

SelfTest::SelfTest() {

}

void SelfTest::SelfTestFailed(ConcreteSystem *cs) {
    ChangeState(cs, Failure::GetInstance());
}

SystemState* SelfTest::GetInstance() {
    if (instance == 0) {
        instance = new SelfTest();
    }
    return instance;
}

Failure.h

#ifndef FAILURE_H_
#define FAILURE_H_

#include "SystemState.h"

class SelfTest;

class Failure : public SystemState {
public:
    Failure();
    void Restart(ConcreteSystem* t);
    static SystemState* GetInstance();
private:
    static SystemState* instance;
};

#endif /* FAILURE_H_ */

Failure.cpp

#include "Failure.h"
#include "SelfTest.h"

SystemState* Failure::instance = 0;

Failure::Failure() {

}

void Failure::Restart(ConcreteSystem* t) {
    ChangeState(t, SelfTest::GetInstance());
}

SystemState* Failure::GetInstance() {
    if (instance == 0) {
        instance = new Failure();
    }
    return instance;
}

I have problem with the includes, which gives me some weird compiler errors. Anyone with a good solution to this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:48:37+00:00Added an answer on May 20, 2026 at 12:48 pm

    From the looks of the code you’ve posted, you’ll have classes being redefined. Looking at your Failure.cpp file, you have:

    #include "Failure.h"
    #include "SelfTest.h"
    

    Which will include both of those files, and each of those files include the SystemState.h file. Since the SystemState.h file is included more than once, it tries to redefine the SystemState class. At the top of each of your header files, you should do something like this:

    // SystemState.h
    #ifndef SystemState_h
    #define SystemState_h
    .. class definition ..
    #endif // close the if statement from above.
    

    As an aside on the design, I think it’s bad form for the states to know about each other – use your ConcreteSystem as a state controller and then base the state on the return value of the last state operation.

    Also, if you’re relatively inexperienced with C++, I would recommend looking at this as a great source of learning material (in addition to StackOverflow, of course!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.