2017-02-27 86 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. 
相關問題