2017-02-27 87 views
0

你好,我有坏消息:我根本不知道发生了什么事。不能抽象类型的对象 - 覆盖不会工作?

我正在尝试制作一堆StarWarsShip对象和一堆StarTrekShip对象,并使它们在链接列表中互相争夺。跳过一堆无聊的细节,C++不会让我创建StarWarsShip/StarTrekShip对象,因为它们是抽象的 - 我只是模糊地知道Google搜索之前的一些含义。

反正这里的一些代码:

StarWarsShip.h

#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 

SpaceShip.h

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(只是构造)

#include "FlightManager.h" 

using namespace std; 

FlightManager::FlightManager(string fileName) 
{ 
    ifstream inFile(fileName); 
    if (inFile.is_open()) 
    { 
    cout << "File found. Reading in...\n"; 
    } 
    else 
    { 
    //quit 
    } 
//now i read in stuff from a file, not actually important 
    while (getline(inFile, line)) 
    { 
    //declare temp variables that will be used to parse and organize data 
    string tUniverse; 
    string tLeader; 
    string tATP; 
    string tPar4; 
    string tPar5; 
    string tPar6; 

    //begin parsing list of data and compiling into objects 
    char c; 
    int j = 0; 
    for(char&c : line) 
    { 
     if(c == ',') j++; 
     else if (c == ' ') 
     { 
     tPar6 += c; 
     continue; 
     } 
     else if (j == 0) tUniverse += c; 
     else if (j == 1) tLeader += c; 
     else if (j == 2) tATP += c; 
     else if (j == 3) tPar4 += c; 
     else if (j == 4) tPar5 += c; 
     else if (j == 5) tPar6 += c; 
    } 

    //Declare a new ship object each time a new line is parsed 
    //Add the ship to the list of ships 

    if (tUniverse == "StarWars") 
    { 
//NEXT LINE BIG ERROR YIKES 
     StarWarsShip newShip = new StarWarsShip(); 
     newShip.setStats(tLeader, stoi(tATP), stoi(tPar4), toBool(tPar5), tPar6); 
     //Parameters are : void StarWarsShip::setStats(string P, int atPwr, int hullMax, bool shlds, string LW) 
     //input format is <universe>,<captain name>,<attack power>,<number of crew>,<max hull value>,<shield status> 

     spaceBattle.addBack(newShip); 
    } 
    else 
    { 
//NEXT LINE BIG ERROR YIKES 
     StarTrekShip* newShip = new StarTrekShip(); 
     newShip->setStats(tLeader, stoi(tATP), stoi(tPar4), stoi(tPar5), toBool(tPar6)); 
     //Parameters are : (string C, int atPwr, int crewNumber, int hullMax, bool shlds) 
     //input format is <universe>,<captain name>,<attack power>,<number of crew>,<max hull value>,<shield status> 

     spaceBattle.addBack(newShip); 
    } 

    length = spaceBattle.getLength(); 

    if(inFile.eof()) 
    { 
     break; 
    } 
    } 
    inFile.close(); 
} 

而且我的错误是:

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课程不是抽象的,如果有的话,我做错了什么?

+0

此外,我试图使用覆盖,但当我试图在我的方法声明结束时添加“覆盖”,它只是告诉我它无法覆盖。 – Emrylin

+0

StarWarsShip需要实现所有SpaceShip纯虚函数。否则它不会是新的。别忘了你的纯虚拟funciotn有const后缀。 –

+0

啊好的谢谢。我将const添加到了StarWarsShip方法,但现在我得到了一个不同的错误。 FlightManager.cpp:66:49:error:从'StarWarsShip *'转换为非标量类型'StarWarsShip'请求 StarWarsShip newShip = new StarWarsShip(); – Emrylin

回答

0

StarWarsShip需要实现所有SpaceShip纯虚函数。

不要忘记你的纯虚拟funciotn有const后缀。

所以在子类中也需要const。

最后,新的StarWarsShip()返回StarWarsShip的指针,而不是StarWarsShip。

StarWarsShip newShip = new StarWarsShip(); //error 
StarWarsShip newShip;      //change to this. 
相关问题