C++ 无法对抽象类型的对象进行重写';不行?

C++ 无法对抽象类型的对象进行重写';不行?,c++,inheritance,abstract-class,C++,Inheritance,Abstract Class,大家好,我有个坏消息:我根本不知道发生了什么事 我试着制作一堆星际争霸飞船和星际迷航飞船,让它们在一个链表中互相攻击。跳过一堆无聊的细节,C++不让我创建星际争霸/星际旅行对象,因为它们是抽象的——我只是模糊地知道谷歌搜索之前的一对夫妇的意思。 无论如何,这里有一些代码: 星际争霸 #include "SpaceShip.h" class StarWarsShip : public SpaceShip { private: string uni; //this ship is in the

大家好,我有个坏消息:我根本不知道发生了什么事

我试着制作一堆星际争霸飞船和星际迷航飞船,让它们在一个链表中互相攻击。跳过一堆无聊的细节,C++不让我创建星际争霸/星际旅行对象,因为它们是抽象的——我只是模糊地知道谷歌搜索之前的一对夫妇的意思。 无论如何,这里有一些代码:

星际争霸

#include "SpaceShip.h"

class StarWarsShip : public SpaceShip
{
private:
  string uni; //this ship is in the star wars universe
  string pilot; //this is the captains name
  int atp; //this is how much damage the ship will do
  int hullM; //this is the strength of the ship initially
  int hullC; //this integer keeps track of how much more damage the ship can take
  bool shields; //are the shields up?
  //string shipDeath; //The message that will display when the ship is destroyed
  string lastWords; //The last words of the Star Wars pilot

public:
  StarWarsShip();
  ~StarWarsShip();

  void setStats(string P, int atPwr, int hullMax, bool shlds, string LW); //sets stats of the ship

  string getLeader(); //returns the name of the pilot
  int getAttackPower(); //returns how much attack power the ship has
  int getCurrentHull(); //returns how much health the hull still has? hull? health? hullth?
  int getMaxHull(); //gets the maximum hull value of the ship
  bool takeDamage(int amount); //takes damage if the ship has the hull value to handle it
  bool getShields();  //lets the object know if the shields are down
  string getUniverse();//ship is in the star wars universe
  //string getStatus(); //prints the status of the ship
  //string finalMessage(); //prints the final message of the ship
};
#endif
太空船

class SpaceShip
{
public:
  virtual ~SpaceShip() {};
  virtual string getLeader() const = 0;
  virtual int getAttackPower() const = 0;
  virtual int getCurrentHull() const = 0;
  virtual int getMaxHull() const = 0;
  virtual bool takeDamage(int amount) const = 0;
  virtual bool getShields() const = 0;
  virtual string getUniverse() const = 0;
  //virtual string getStatus() const = 0;
  //virtual string finalMessage() const = 0;
};
#endif
FlightManager.cpp(但仅限于构造函数)


然后它继续烤我的代码。我需要让我的星际飞船类不抽象吗?如果是这样,我做错了什么?

星际飞船需要实现所有飞船纯虚拟功能

别忘了你的纯虚函数有常量后缀

所以在子类中也需要常量

最后,new StarWarsShip()返回一个StarWarsShip而不是StarWarsShip的指针

StarWarsShip newShip = new StarWarsShip(); //error
StarWarsShip newShip;                      //change to this.

此外,我还试图使用覆盖,但当我试图在方法声明的末尾添加“覆盖”时,它只是告诉我它无法覆盖。星战飞船需要实现所有飞船纯虚拟功能。否则它无法实现新功能。别忘了你的纯虚拟函数有常量后缀。啊,好的,谢谢。我在我的星战方法中加入了常数,但是现在我得到了一个不同的错误。FlightManager.cpp:66:49:错误:从“星际飞船*”转换为非标量类型“星际飞船”请求的星际飞船新闻飞船=新星际飞船();你能自己修理吗?或者你可以修改你的问题,添加错误信息。new StarWarsShip()返回星战飞船而不是星战飞船的指针。
FlightManager.cpp:66:49: error: cannot allocate an object of abstract type ‘StarWarsShip’
         StarWarsShip newShip = new StarWarsShip();
In file included from FlightManager.h:10:0,
                 from FlightManager.cpp:8:
StarWarsShip.h:16:7: note:   because the following virtual functions are pure within ‘StarWarsShip’:
 class StarWarsShip : public SpaceShip
       ^
In file included from StarWarsShip.h:14:0,
                 from FlightManager.h:10,
                 from FlightManager.cpp:8:
SpaceShip.h:18:18: note:        virtual std::string SpaceShip::getLeader() const
   virtual string getLeader() const = 0;
StarWarsShip newShip = new StarWarsShip(); //error
StarWarsShip newShip;                      //change to this.