2017-02-27 8 views
0

Howdy fellas私には悪い知らせがあります:何が起こっているのか分かりません。抽象型オーバーライドのオブジェクトは機能しませんか?

私はたくさんの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

ああありがとう。私はStarWarsShipメソッドにconstを追加しましたが、今は別のエラーが発生しています。 FlightManager.cpp:66:49:エラー: '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. 
関連する問題