构造函数/实验9问题

时间:2017-05-07 09:41:50

标签: c++ visual-studio visual-studio-2010 c++11 visual-c++

我的教授希望我们创建一个角斗士模拟,我们命名5个角斗士,然后创建他们的统计数据,创建boss统计数据,然后允许角斗士与老板战斗。在战斗期间,每个人的健康状况和随机产生的数字伤害将被显示,直到决定胜利者为止,我们将提示用户是否需要重赛。

目前,我一直在搞清楚是什么以及如何使用构造函数。总的来说,我总是迷失在这个项目中,但是现在我想逐步理解这个。在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未解析的外部符号“private:void __thiscall   BossFight :: getParty(void)“(?getParty @BossFight @@ AAEXXZ)在   功能“public:__ thistall BossFight :: BossFight(void)”   (?? 0BossFight @@ QAE @ XZ)ConsoleApplication6严重级代码描述项目文件行抑制   状态错误LNK2019未解析的外部符号“private:void   __thiscall BossFight :: getParty(void)“(?getParty @BossFight @@ AAEXXZ)在函数”public:__thiscall BossFight :: BossFight(void)“中引用   (?? 0BossFight @@ QAE @ XZ)ConsoleApplication6 C:\ Users \ 1 \ documents \ visual   studio 2017 \ Projects \ ConsoleApplication6 \ ConsoleApplication6 \ 1.obj 1

想知道是什么导致了这个?我在visual studio中的所有内容都是我的标题和cpp文件。

1 个答案:

答案 0 :(得分:0)

这称为链接器错误。错误是说BossFight :: getParty()是由BossFight :: BossFight()构造函数实现使用的,但是你还没有提供getParty()方法的实现。

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

要实现BossFight :: getParty(),您需要以下内容:

[{year: 2012, count:2}, {year: 2013, count:1}]

您可能还想通过给它命名来挂起您构造的void BossFight::getParty() { // implementation here } 对象:

BossFight