2017-02-27 1 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

아 감사합니다. StarWarsShip 메서드에 const를 추가했지만 이제는 다른 오류가 발생합니다. FlightManager.cpp : 66 : 49 : 오류 : 'StarWarsShip *'에서 비 스칼라 유형 'StarWarsShip'으로의 변환이 요청되었습니다. StarWarsShip newShip = new StarWarsShip(); – Emrylin

답변

0

StarWarsShip은 모든 SpaceShip 순수 가상 기능을 구현해야합니다.

순수 가상 funciotn에 const 접미사가 있음을 잊지 마십시오.

그래서 하위 클래스에서도 const가 필요합니다.

마지막으로 새 StarWarsShip()은 StarWarsShip이 아닌 StarWarsShip의 포인터를 반환합니다.

관련 문제