我在为我的项目实现策略模式时遇到问题。我创建了所有必需的文件,但是由于main的新调用似乎无法将策略初始化为所需的内容,因此出现错误。

策略

/*All the classes that implement a concrete strategy should use this
The AI class will use this file as a concrete strategy
*/
using namespace std;
class Strategy{
public:
    //Method who's implementation varies depending on the strategy adopted
    //Method is therefore virtual
    virtual int execute() = 0;
 };

我的三个策略
好斗的
#pragma once
#include <iostream>
#include "Strategy.h"

class Aggressive{
   public:
      int execute();

 };

好斗的
#pragma once
#include <iostream>
#include "Strategy.h"
using namespace std;

class Aggressive : public Strategy{
  public:
      Aggressive(){}

      int execute(){
        cout << "The defensive player chooses to adopt an aggressive play-                             style" << endl;
        return 0;
    }

};

防守h
#pragma once
#include <iostream>
#include "Strategy.h"

class Defensive{
  public:
     int execute();

};

Defensive.cpp
#include <iostream>
#include "Strategy.h"
using namespace std;

 class Defensive : public Strategy{
    public:
       int execute(){
        cout << "The defensive player chooses to adopt a defensive play-style" << endl;
    }
 };

人工智能
#pragma once
#include "Strategy.h"

class AI{
  public:
    AI();
    AI(Strategy *initStrategy);
    void setStrategy(Strategy *newStrategy);
    int executeStrategy();

};

人工智能
#pragma once
#include "Strategy.h"
#include "AI.h"
#include "Aggressive.h"
#include "Defensive.h"


using namespace std;

 class AI{
   private:
       Strategy *strategy;
   public:
      AI(){}
         //Plugs in specific strategy to be adopted
      AI(Strategy *initStrategy){
          this->strategy = initStrategy;
      }
      void setStrategy(Strategy *newStrategy){
          this->strategy = newStrategy;
      }
      //Method that executes a different strategy depending on what
      //strategt was plugged in upon instantiation.
       int executeStrategy(){
          return this->strategy->execute();
      }
  };

还有我的临时驱动程序,它与新驱动程序有关
StrategyDriver.cpp
#pragma once
#include "AI.h"
#include "Strategy.h"
#include "Aggressive.h"
#include "Defensive.h"
#include "Random.h"
using namespace std;

int main(){
   AI *ai(new Aggressive());
    ai->executeStrategy();
}

如果有人看到我的代码有问题,将不胜感激。我不确定如何将新的AI初始化为已实现的特定策略。

最佳答案

您试图两次定义相同的类,一次在标题中,一次在源文件中。 main仅在 header 中看到定义,该定义不继承自Strategy,因此是错误。

header 需要定义该类,并声明其所有父类和成员:

#pragma once
#include "Strategy.h"

class Aggressive : public Strategy{
public:
    Aggressive(){}
    int execute();
};

源文件只定义了 header 中未定义的任何成员:
// no header guard (#pragma once) - that's just for headers
#include <iostream>
#include "Aggressive.h"  // include the class definition
using namespace std;

int Aggressive::execute(){
    cout << "The defensive player chooses to adopt an aggressive play-                             style" << endl;
    return 0;
}

进行这些更改后,类定义将在main中使用的位置可用,因此将允许从Aggressive*转换为基类指针Strategy*。您需要对其他类进行类似的更改,以使程序正确构建和运行。

最后,main想创建一个AI对象(可以使用Strategy*初始化),而不是AI*指针(不能)。
AI ai(new Aggressive());
ai.executeStrategy();

10-08 00:34