c++ 错误,未在State设计模式中命名类型[已关闭]

nvbavucw  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(120)

**已关闭。**此问题不符合Stack Overflow guidelines。当前不接受答案。

这个问题似乎与help center中定义的范围内的编程无关。
2天前关闭。
Improve this question
我对C++比较陌生。我正在为物联网应用程序应用状态设计模式,该模式只需每15秒轮询一个传感器,然后如果它仍然在线,则将数据发送到某个地方,如果它不在线,然后将其保存到一个数组中,然而,我已经在这段代码上花了几天时间,当我在OfflineState中添加TransitionTo时,我遇到了错误。据我所知是因为TransitionTo(new OnlineState),OnlineState是在我尝试使用函数之后声明的,但是如果我更改声明顺序,则会在TransitionTo中得到错误(new OfflineState).来自C,我希望只是做一些类似于添加原型的事情,在本例中,我尝试Forward声明状态类,但是它不起作用。我想知道为什么它是错误的面向对象的明智和如何修复它。

#include <iostream>
#include <typeinfo>
#include <WiFi.h>
#include <WiFiClientSecure.h>
// The base State class declares methods that all Concrete State should
// implement and also provides a backreference to the Context object, associated
// with the State. This backreference can be used by States to transition the
// Context to another State.
const char *ssid = "XXX";
const char *password = "XXX";
unsigned long lastTime = 0;
const unsigned long timerDelay = 15000;
int flag = 0;
const TickType_t connectionDelay = 1500 / portTICK_PERIOD_MS;

WiFiClientSecure client;

class State
{
  // @var Context
protected:
  Context * context_;

public:
  virtual ~ State ()
  {
  }

  void set_context (Context * context)
  {
    this->context_ = context;
  }

  virtual void Connect () = 0;
  virtual void CheckConnection () = 0;
  virtual void setMyAttribute (float x) = 0;

};

// The Context defines the interface of interest to clients. It also maintains a
// reference to an instance of a State subclass, which represents the current
// state of the Context.

class Context
{
  // @var State A reference to the current state of the Context.
private:
  State * state_;

public:
  float sensorData = 0;

    Context (State * state):state_ (nullptr)
  {
    this->TransitionTo (state);
  }
   ~Context ()
  {
    delete state_;
  }

  // The Context allows changing the State object at runtime.
  void TransitionTo (State * state)
  {
    Serial.println ("Transitioning");
    if (this->state_ != nullptr)
      delete this->state_;
    this->state_ = state;
    this->state_->set_context (this);
  }

  // The Context delegates part of its behavior to the current State object.
  void Connect ()
  {
    this->state_->Connect ();
  }
  void CheckConnection ()
  {
    this->state_->CheckConnection ();
  }

  void setMyAttribute (float x)
  {
    this->state_->setMyAttribute (x);
  }

};

// Concrete States implement various behaviors, associated with a state of the Context

class OfflineState:public State
{
public:
  virtual void Connect () override
  {
    Serial.println ("connect from offlineState called");
    WiFi.begin (ssid, password);
    Serial.print ("Connecting to wifi...");
    for (int i = 0; i < 10; i++)
      {
    if (WiFi.status () != WL_CONNECTED)
      {
        Serial.print ('.');
        vTaskDelay (connectionDelay);
      }
    else
      {
        i = 10;
        Serial.println ();
        Serial.println ("Connected !");
        this->context_->TransitionTo (new OnlineState);
      }
      }
  }

  void CheckConnection () override
  {

    Serial.println ("Check connection from offlinestate called");
  }

  void setMyAttribute (float x) override
  {
    CheckConnection ();
  }

};

class OnlineState:public State
{
public:
  void Connect () override
  {
    Serial.println ("Already connected");
  }

  void CheckConnection () override
  {
    Serial.println ("Check connection from onlineState called");
    if (WiFi.status () != WL_CONNECTED)
      {
    Serial.println ("Disconnected!");
    this->context_->TransitionTo (new OfflineState);
      }
  }

  void setMyAttribute (float x) override
  {
    CheckConnection ();

  }

};

Context *context = new Context (new OfflineState);

void
setup ()
{

  WiFi.mode (WIFI_STA);
  lastTime = millis ();
  Serial.begin (115200);
  context->Connect ();
  context->CheckConnection ();
}

void
loop ()
{
  if (millis () > (lastTime + timerDelay))
    {

      context->CheckConnection ();
      lastTime = millis ();
    }
}

我试过使Connect()虚空,以便在两个状态之后声明它。

void OnlineState::Connect() {
{
std::cout << "OnlineState: Already connected.\n";
}
}

但是我仍然会得到错误,因为在类声明中,类看起来是抽象的。

41zrol4v

41zrol4v1#

在编译器看到new OnlineState之前,您无法执行该操作

class OnlineState:public State

这里要做的是创建头文件:

  • State.h
  • OfflineState.h
  • OnlineState.h

void CheckConnection () override;为每个.h文件创建.cpp文件,并将定义放入其中:

void OfflineState::CheckConnection()
{
    // ...
}

为每个源文件给予必要的头文件。

相关问题