2015-07-19 130 views
0

當我啓動我的遊戲時,它在我調用構造函數時似乎崩潰了。它所說的是「a.exe已停止工作」,然後我必須關閉該程序。我試圖通過我所有的鏈接類和標題追溯問題,但是我看不到任何錯誤。也許你們中的一個可以幫助我。下面是所涉及的類:程序在發佈時崩潰

#ifndef POKEMON_H_ 
#define POKEMON_H_ 

#include "MoveList.cpp" 
#include "Elements.cpp" 

    class Pokemon 
    { 
    protected: 
     std::string name; 
     double HP; 
     vector<Move> potentialMoves; 
     vector<Move> moves; 
     int attack; 
     int defense; 
     int speed; 
     int special; 
     Elements element; 
     std::string type; 
     char status; // (N)ormal, (B)urnt, (F)rozen, E - Paralyzed, (P)oisoned, (S)leep, (C)onfused 
     // int ability; 
     int ID; 
     int level; 
     double experience; 
     bool isInAir; 
     bool isInGround; 
    public: 
     Pokemon(double HP, int attack, int defense, int speed, int special, int elementNumber, std::string type, int ID, std::string name); 
     ~Pokemon(); 
     virtual void setName(string x); 
     virtual string getName(); 
     virtual void setHP(double x); 
     virtual double getHP(); 
     virtual void setPotentialMoves(vector<Move> x); 
     virtual vector<Move> getPotentialMoves(); 
     virtual Move& getPotentialMove(int x); 
     virtual void setAttack(int x); 
     virtual int getAttack(); 
     virtual void setDefense(int x); 
     virtual int getDefense(); 
     virtual void setSpeed(int x); 
     virtual int getSpeed(); 
     virtual void setSpecial(int x); 
     virtual int getSpecial(); 
     virtual Elements& getElement(); 
     virtual string getType(); 
     virtual void setStatus(char x); 
     virtual char getStatus(); 
     virtual int getID(); 
     virtual void setLevel(int x); 
     virtual int getLevel(); 
     virtual void setExperience(double x); 
     virtual double getExperience(); 
     virtual void setAir(bool x); 
     virtual bool getAir(); 
     virtual void setGround(bool x); 
     virtual bool getGround(); 
     virtual void setMove(const Move& move, int x); 
     virtual Move& getMove(int x); 
     virtual void attackMove(int moveNumber, Pokemon& opponent); 
    }; 

     class Bulbasaur: public Pokemon 
     { 
     public: 
      Bulbasaur(double HP, int attack, int defense, int speed, int special, string name); 
      ~Bulbasaur(); 
     }; 

     class Ivysaur: public Pokemon 
     { 
     public: 
      Ivysaur(double HP, int attack, int defense, int speed, int special, string name); 
      ~Ivysaur(); 
     }; 

     class Venosaur: public Pokemon 
     { 
     public: 
      Venosaur(double HP, int attack, int defense, int speed, int special, string name); 
      ~Venosaur(); 
     }; 

     #endif 

#include "Pokemon.h" 
#include <cstdlib> 

using namespace std; 

Pokemon::Pokemon(double HP, int attack, int defense, int speed, int special, int elementNumber, string type, int ID, string name): HP(HP), attack(attack), 
       defense(defense), speed(speed), special(special), type(type), ID(ID), name(name), element(element) 
       { 
        ElementsList tempList = ElementsList(); 
        moves.push_back(Move()); 
        moves.push_back(Move()); 
        moves.push_back(Move()); 
        moves.push_back(Move()); 
        status = 'N'; 
        level = 1; 
        experience = 0.0; 
        isInAir = false; 
        isInGround = false; 
        element = tempList.getElement(elementNumber); 
        tempList.~ElementsList(); 
       } 

Pokemon::~Pokemon() {} 

void Pokemon::setName(string x) 
{ 
    this->name = x; 
} 

string Pokemon::getName() 
{ 
    return this->name; 
} 

void Pokemon::setHP(double x) 
{ 
    this->HP = x; 
} 

double Pokemon::getHP() 
{ 
    return this->HP; 
} 

void Pokemon::setPotentialMoves(vector<Move> x) 
{ 
    for(int i = 0; i < x.size(); i++) 
    { 
     potentialMoves.push_back(x[i]); 
    } 
} 

vector<Move> Pokemon::getPotentialMoves() 
{ 
    return this->potentialMoves; 
} 

Move& Pokemon::getPotentialMove(int x) 
{ 
    return this->potentialMoves[x]; 
} 

void Pokemon::setAttack(int x) 
{ 
    this->attack = x; 
} 

int Pokemon::getAttack() 
{ 
    return this->attack; 
} 

void Pokemon::setDefense(int x) 
{ 
    this->defense = x; 
} 

int Pokemon::getDefense() 
{ 
    return this->defense; 
} 

void Pokemon::setSpeed(int x) 
{ 
    this->speed = x; 
} 

int Pokemon::getSpeed() 
{ 
    return this->speed; 
} 

void Pokemon::setSpecial(int x) 
{ 
    this->special = x; 
} 

int Pokemon::getSpecial() 
{ 
    return this->special; 
} 

Elements& Pokemon::getElement() 
{ 
    return this->element; 
} 

string Pokemon::getType() 
{ 
    return this->type; 
} 

void Pokemon::setStatus(char x) 
{ 
    this->status = x; 
} 

char Pokemon::getStatus() 
{ 
    return this->status; 
} 

int Pokemon::getID() 
{ 
    return this->ID; 
} 

void Pokemon::setLevel(int x) 
{ 
    this->level = x; 
} 

int Pokemon::getLevel() 
{ 
    return this->level; 
} 

void Pokemon::setExperience(double x) 
{ 
    this->experience = x; 
} 

double Pokemon::getExperience() 
{ 
    return this->experience; 
} 

void Pokemon::setAir(bool x) 
{ 
    this->isInAir = x; 
} 

bool Pokemon::getAir() 
{ 
    return this->isInAir; 
} 

void Pokemon::setGround(bool x) 
{ 
    this->isInGround = x; 
} 

bool Pokemon::getGround() 
{ 
    return this->isInGround; 
} 

void Pokemon::setMove(const Move& move, int x) 
{ 
    this->moves[x] = move; 
} 

Move& Pokemon::getMove(int x) 
{ 
    return this->moves[x]; 
} 

void Pokemon::attackMove(int moveNumber, Pokemon& opponent) 
{ 
    Move currentMove = this->getMove(moveNumber); 
    double damageDealt = opponent.getHP() - currentMove.getHPdiff(); 
    int attackDifference = opponent.getAttack() - currentMove.getattackDiff(); 
    int defenseDifference = opponent.getDefense() - currentMove.getdefenseDiff(); 
    int speedDifference = opponent.getSpeed() - currentMove.getspeedDiff(); 
    int specialDifference = opponent.getSpecial() - currentMove.getspecialDiff(); 
    char statusDifference = currentMove.getstatusDiff(); 
    bool isInAirDifference = currentMove.getisInAirDiff(); 
    bool isInGroundDifference = currentMove.getisInGroundDiff(); 
    opponent.setHP(damageDealt); 
    opponent.setAttack(attackDifference); 
    opponent.setDefense(defenseDifference); 
    opponent.setSpeed(speedDifference); 
    opponent.setSpecial(specialDifference); 
    opponent.setStatus(statusDifference); 
    this->setAir(isInAirDifference); 
    this->setGround(isInGroundDifference); 
} 

Bulbasaur::Bulbasaur(double HP, int attack, int defense, int speed, int special, string name): 
      Pokemon(HP, attack, defense, speed, special, 4, "Seed", 1, name) 
      { 
       BulbasaurMoveList temp = BulbasaurMoveList(); 
       this->setPotentialMoves(temp.getPotentialMoves()); 
       temp.~BulbasaurMoveList(); 
       vector<Move> temp2; 
       for(int i = 0; i < this->getPotentialMoves().size(); i++) 
       { 
        temp2.push_back(this->getPotentialMove(i)); 
       } 
       vector<int> usedNums; 
       srand(0); 
       int i = 0; 
       while(i < 4) 
       { 
        int r = rand() % 4; 
        if(i == 0) 
        { 
         usedNums.push_back(r); 
        } else 
        { 
         for(int j = 0; j < usedNums.size(); j++) 
         { 
          if(usedNums[j] == r) 
          { 
           continue; 
          } 
         } 
         usedNums.push_back(r); 
        } 
        this->setMove(temp2[r], i); 
        i++; 
       } 
      } 

這裏是MoveList.cpp:

#include "MoveList.h" 
#include <iostream> 
#include <vector> 

using namespace std; 

Move::Move(string name, string type, int PP, double HPdiff, int attackDiff, int defenseDiff, int speedDiff, int specialDiff, 
     char statusDiff, bool isInAirDiff, bool isInGroundDiff): name(name), type(type), PP(PP), HPdiff(HPdiff), attackDiff(attackDiff), 
    defenseDiff(defenseDiff), speedDiff(speedDiff), specialDiff(specialDiff), statusDiff(statusDiff), isInAirDiff(isInAirDiff), 
    isInGroundDiff(isInGroundDiff) {} 

Move::Move(): name(), type(), PP(), HPdiff(), attackDiff(), defenseDiff(), speedDiff(), specialDiff(), statusDiff(), isInAirDiff(), 
      isInGroundDiff() {} 

string Move::getName() 
{ 
    return this->name; 
} 

string Move::getType() 
{ 
    return this->type; 
} 

int Move::getPP() 
{ 
    return this->PP; 
} 

double Move::getHPdiff() 
{ 
    return this->HPdiff; 
} 

int Move::getattackDiff() 
{ 
    return this->attackDiff; 
} 

int Move::getdefenseDiff() 
{ 
    return this->defenseDiff; 
} 

int Move::getspeedDiff() 
{ 
    return this->speedDiff; 
} 

int Move::getspecialDiff() 
{ 
    return this->specialDiff; 
} 

char Move::getstatusDiff() 
{ 
    return this->statusDiff; 
} 

bool Move::getisInAirDiff() 
{ 
    return this->isInAirDiff; 
} 

bool Move::getisInGroundDiff() 
{ 
    return this->isInGroundDiff; 
} 

MoveList::MoveList() 
{ 
    masterList.push_back(Move("Pound", "NORMAL", 35, 8.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Karate Chop", "FIGHTING", 25, 10.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Double Slap", "NORMAL", 15, 3.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Comet Punch", "NORMAL", 15, 3.6, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Mega Punch", "NORMAL", 20, 16.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Pay Day", "NORMAL", 20, 8.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Fire Punch", "FIRE", 15, 15.0, 0, 0, 0, 0, 'B', false, false)); 
    masterList.push_back(Move("Ice Punch", "ICE", 15, 15.0, 0, 0, 0, 0, 'F', false, false)); 
    masterList.push_back(Move("Thunder Punch", "ELECTRIC", 15, 15.0, 0, 0, 0, 0, 'E', false, false)); 
    masterList.push_back(Move("Scratch", "NORMAL", 35, 8.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Vice Grip", "NORMAL", 30, 11.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Guillotine", "NORMAL", 5, 100.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Razor Wind", "NORMAL", 10, 20.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Swords Dance", "NORMAL", 20, 0.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Cut", "NORMAL", 30, 10.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Gust", "FLYING", 35, 8.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Wing Attack", "FLYING", 35, 12.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Whirlwind", "NORMAL", 20, 0.0, 0, 0, 0, 0, 'N', false, false)); 
    masterList.push_back(Move("Fly", "NORMAL", 15, 18.0, 0, 0, 0, 0, 'N', true, false)); 
    masterList.push_back(Move("Bind", "NORMAL", 20, 3.0, 0, 0, 0, 0, 'N', false, false)); 
} 

MoveList::~MoveList() {} 

vector<Move> MoveList::getList() 
{ 
    return this->masterList; 
} 

BulbasaurMoveList::BulbasaurMoveList() 
{ 
    MoveList y = MoveList(); 
    vector<Move> z = y.getList(); 
    potentialMoves.push_back(z[0]); 
    potentialMoves.push_back(z[9]); 
    potentialMoves.push_back(z[10]); 
    potentialMoves.push_back(z[14]); 
    y.~MoveList(); 
} 

BulbasaurMoveList::~BulbasaurMoveList() {} 

vector<Move> BulbasaurMoveList::getPotentialMoves() 
{ 
    return potentialMoves; 
} 

IvysaurMoveList::IvysaurMoveList() 
{ 
    MoveList y = MoveList(); 
    vector<Move> z = y.getList(); 
    potentialMoves.push_back(z[0]); 
    potentialMoves.push_back(z[9]); 
    potentialMoves.push_back(z[10]); 
    potentialMoves.push_back(z[14]); 
    y.~MoveList(); 
} 

IvysaurMoveList::~IvysaurMoveList() {} 

vector<Move> IvysaurMoveList::getPotentialMoves() 
{ 
    return potentialMoves; 
} 

VenosaurMoveList::VenosaurMoveList() 
{ 
    MoveList y = MoveList(); 
    vector<Move> z = y.getList(); 
    potentialMoves.push_back(z[0]); 
    potentialMoves.push_back(z[9]); 
    potentialMoves.push_back(z[10]); 
    potentialMoves.push_back(z[14]); 
    y.~MoveList(); 
} 

VenosaurMoveList::~VenosaurMoveList() {} 

vector<Move> VenosaurMoveList::getPotentialMoves() 
{ 
    return potentialMoves; 
} 

Elements.cpp:

#include "Elements.h" 
#include <string> 
#include <vector> 
#include <iostream> 

using namespace std; 

Elements::Elements(string name): name(name) {} 

Elements::Elements() {} 

ElementsList::ElementsList() 
{ 
    masterList.push_back(Elements("NORMAL")); 
    masterList.push_back(Elements("FIRE")); 
    masterList.push_back(Elements("WATER")); 
    masterList.push_back(Elements("ELECTRIC")); 
    masterList.push_back(Elements("GRASS")); 
    masterList.push_back(Elements("ICE")); 
    masterList.push_back(Elements("FIGHTING")); 
    masterList.push_back(Elements("POISON")); 
    masterList.push_back(Elements("GROUND")); 
    masterList.push_back(Elements("FLYING")); 
    masterList.push_back(Elements("PSYCHIC")); 
    masterList.push_back(Elements("BUG")); 
    masterList.push_back(Elements("ROCK")); 
    masterList.push_back(Elements("GHOST")); 
    masterList.push_back(Elements("DRAGON")); 
} 

ElementsList::~ElementsList() {} 

Elements ElementsList::getElement(int x) 
{ 
    return masterList[x]; 
} 

然後主要功能:

#include "Pokemon.cpp" 
#include <iostream> 
#include <limits> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 
    cout << "Battle Test 1: Bulbasaur vs. Ivysaur Same Level" << endl; 
    cout << "test" << endl; 
    try 
    { 
     Bulbasaur test1 = Bulbasaur(300.0, 20, 30, 10, 5, "Chris"); 
    } catch(exception& e) 
    { 
     cout << e.what() << endl; 
    } 
} 
+0

你爲什麼要手動調用所有這些析構函數? –

+1

請創建一個[最小,完整,可驗證示例](http://stackoverflow.com/help/mcve)讓回答者瞭解問題。 (儘管在這種情況下,我會說最好的辦法是簡單地在違規構造函數的第一行放置一個斷點,看看哪條線路崩潰了,這將爲您提供足夠的方向知道如何解決問題。) –

+0

我很抱歉,什麼是斷點?我是一個新手.. – Russell

回答

0

因爲你打電話給d在對象上手動構造器。這是非常罕見的需要做到這一點(見calling destructor explicitly)。當你手動調用你的析構函數時,當對象超出範圍時它仍然會被再次調用。舉個例子:

BulbasaurMoveList::BulbasaurMoveList() 
{ 
    MoveList y = MoveList(); 
    vector<Move> z = y.getList(); 
    potentialMoves.push_back(z[0]); 
    potentialMoves.push_back(z[9]); 
    potentialMoves.push_back(z[10]); 
    potentialMoves.push_back(z[14]); 
    y.~MoveList(); 
} 

y被摧毀了兩次,而這results in undefined behaviour

調用這個對象的生命週期已經結束未定義行爲每C++ 03§12.4結果的析構函數/ 6:

如果析構函數被調用一個對象,其壽命已經結束的行爲是未定義

+0

你真棒,謝謝你。今天學到了新東西。這種東西我永遠不會知道,因爲它不在我的書中使用。 – Russell