2017-05-07 40 views
0

我的教授希望我们能够创造一个斗士模拟,我们命名5角斗士,然后创建自己的统计数据,建立统计老大,然后允许角斗士打BOSS。在战斗中,每个人的健康状况和随机生成的数字伤害都会显示,直到获胜者决定为止,然后我们会提示用户是否需要复赛。构造函数/实验室9问题

目前,我停留在搞清楚什么是以及如何使用构造函数。总的来说,我对这个项目感到迷茫,但现在我想逐步理解这一点。在BossFight.h中,包含原型函数。

class BossFight { 
private: 
    //Holds the party of gladiators that is banded together to fight the boss 
    Gladiator party[PSIZE]; 
    //Holds the powerful boss that the party is fighting 
    Gladiator boss; 
    //Variables used for record keeping 
    int turnNum, fightsStarted, fightsWon; 
    //Will fill the party with gladiators, this function can call/reuse the createGladiator function. 
    void getParty(); 
    //Will generate a boss for the party to fight. Has no crit or evasion, but 3* damage min and range, and 6* health 
    void getBoss(); 
    //Tells the user who won, after how many turns, and the current record for the session 
    void displayFightResults(bool partyWon); 
    //One turn occurs, where each party member attacks the boss, then the boss attacks the party. 
    //Returned value indicates status of fight (continue, party win, party loss) 
    //Boss will randomly choose between attacking a single (randomly chosen) party member for full damage, or 
    //attacking the full party for half damage. 
    int takeTurn(); 
    //Handles dealing damage to the entire party 
    void bossAttacksArea(); 
public: 
    //Responsible for generating the party and the boss, should initialize the other 
    //private variables as well 
    BossFight(); 
    //User calls this when they want to run a fight. It will ask them if they want to use 
    //the same party, or get a new one. 
    void runFight(); 
}; 

我迄今所做的是

#include <iostream> 
#include <string> 
#include "BossFight.h" 
#include <stdlib.h> 
#include <time.h> // Allows seed to generate new random numbers every time. 
using namespace std; 

const int SIZE = 5; //Party Size 


Gladiator createGladiator(string name) // Data type Gladiator with its data named createGladiator 
{ 
    Gladiator stats; // Structure tag 
    for (int i = 0; i < SIZE; i++) 
    { 
     int maxHealth, evasion, critical; 
     stats.name = name; 

     // set max health 
     switch (rand() % 3) // % 3 means the range. So the starting number is 0 and final number is 2. Used to find a random number between the range 0-2. 
          // Uses that random number to open up one of the cases. 
     { 
     case 0: stats.maxHealth = 150; 
      break; 
     case 1: stats.maxHealth = 200; 
      break; 
     case 2: stats.maxHealth = 250; 
      break; 
     } 
     // set evasion 


     int numE = (rand() % 5); // Used to find a random number between the range 0-4. 
     switch (numE) // Uses that random number to open up one of the cases. 
     { 
     case 0: stats.evasion = 50; 
      break; 
     case 1: stats.evasion = 75; 
      break; 
     case 2: stats.evasion = 100; 
      break; 
     case 3: stats.evasion = 125; 
      break; 
     case 4: stats.evasion = 150; 
      break; 
     } 

     // Set Critical 

     int numC = (rand() % 5); // Used to find a random number between the range 0-4. 
     switch (numC) // // Uses that random number to open up one of the cases. 
     { 
     case 0: stats.critical = 50; 
      break; 
     case 1: stats.critical = 75; 
      break; 
     case 2: stats.critical = 100; 
      break; 
     case 3: stats.critical = 125; 
      break; 
     case 4: stats.critical = 150; 
      break; 
     } 

     // Set minDamage 
     int minimum, maximum; 
     minimum = 8; 
     maximum = 5; 
     int numMin = (minimum + rand() % (maximum + minimum)); // Used to find a random number between the minimum and maximum values. 
     stats.dmgMin = numMin; 

     // set DamageRange 
     int maxMin, maxMax; 
     maxMin = 16; 
     maxMax = 5; 
     int numMax = (maxMin + rand() % (maxMax - maxMin)); // Used to find a random number between the minimum and maximum values. 
     stats.dmgRange = numMax; 

     return stats; //Return all of the stats into the structure tag. 
    } 
} 


BossFight::BossFight() ***< -- stuck right here *** 
{ 
    getParty(); 
} 

void BossFight::getBoss() 
{ 
    getBoss(); 
} 
void getParty(string name[]) 
{ 

    { 
     cout << "To begin with, enter 5 gladiator's name" << endl; // First for loop asking user for array input. 
     for (int i = 0; i < SIZE; i++) 
     { 
      cin >> name[i]; 
     } 
     for (int i = 0; i < SIZE; i++) 
     { 
      cout << "Gladiator " << i + 1 << " name is " << endl; 
      cout << name[i] << endl; 
     } 
    } 
} 

int main() 
{ 
    srand(time(NULL)); //initiate random number generator seed. 

    string name[SIZE]; 
    cout << "Hello user" << endl; 

    BossFight(); 

      system("PAUSE"); 
} 

我将不胜感激任何类型的帮助。请记住我正在介绍计算机科学课,所以我可能还不了解复杂的编码。到目前为止,我一直在很好地解释代码应该如何用英文工作,但是很难解释代码应该如何工作。

此外,我得到一个错误

严重性代码说明项目文件的线路抑制状态 错误LNK2019解析的外部符号 “私人:无效__thiscall BossFight :: getParty(无效)”?(getParty @ BossFight @@ AAEXXZ)参考 函数“public:__thiscall BossFight :: BossFight(void)” (?? 0BossFight @@ QAE @ XZ)ConsoleApplication6严重级代码说明项目文件行抑制 状态错误LNK2019无法解析的外部符号“private:void __thiscall BossFight :: getParty(void)“(?getParty @ BossFight @@ A AEXXZ)在函数“public:__thiscall BossFight :: BossFight(void)”中引用ConsoleApplication6 C:\ Users \ 1 \ documents \ visual studio 2017 \ Projects \ ConsoleApplication6 \ ConsoleApplication6 \ 1 .OBJ 1

,并想知道什么,甚至造成的?我所有的视觉工作室都是我的头文件和cpp文件。

回答

0

这就是所谓的连接错误。什么是错误的意思是,BossFight :: getParty()正在使用由BossFight :: BossFight()构造函数实现,但你没有提供的getParty()方法的实现。

它看起来像你试图通过声明和实现getParty(std :: string *)函数来添加实现,但这不是BossFight :: getParty()方法的实现。

要实现BossFight :: getParty(),您将需要类似:

void BossFight::getParty() { 
    // implementation here 
} 

你可能也想挂到,你给它一个名字构建BossFight对象:

BossFight boss_fight; // This declares *and* constructs a BossFight object on the stack. 
+0

太棒了!谢谢您的回复。我仍然有点失落,你的意思是执行什么?我试图做 无效BossFight :: getParty(){ getParty; } 并没有发生任何事情。我认为这是因为我在void函数中调用void函数? –

+0

Hi @TonyDo:“实现”是指当​​调用函数或方法(也称为“类的成员函数”)时执行的代码。您的'BossFight.h'头文件包含一些声明,并且您的CPP文件包含一些实现。您需要编写在其实现中调用BossFight :: getParty()时应运行的任何代码。你应该实现(提供一个)所有BossFight方法。 –

+0

至于你的其他问题,可以在void函数中调用void函数。但你可能不想'void BossFight :: getParty(){getParty(); },因为这会导致无限循环或堆栈溢出。 –