【问题标题】:constructors / Lab 9 Problems [duplicate]构造函数/实验 9 问题 [重复]
【发布时间】:2017-10-05 09:57:49
【问题描述】:

我的教授希望我们创建一个角斗士模拟,我们在其中命名 5 个角斗士,然后创建他们的统计数据,创建 Boss 统计数据,然后让角斗士与 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) 在 函数“公共:__thiscall 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 工作室 2017\Projects\ConsoleApplication6\ConsoleApplication6\1.obj 1

想知道是什么原因造成的?我的视觉工作室中只有我的头文件和 cpp 文件。

【问题讨论】:

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


    【解决方案1】:

    这称为链接器错误。错误的意思是 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.
    

    【讨论】:

    • 太棒了!感谢你的回复。我仍然有点迷茫,你所说的实施是什么意思?我尝试做 void BossFight::getParty() { getParty;并且什么也没发生。我以为那是因为我在 void 函数内部调用了 void 函数?
    • 嗨@TonyDo:“实现”是指调用函数或方法(也称为“类的成员函数”)时执行的代码。您的 BossFight.h 头文件包含一些声明,您的 CPP 文件包含一些实现。您需要编写在其实现中调用 BossFight::getParty() 时应该运行的任何代码。您应该实现(提供一个实现)所有 BossFight 方法。
    • 至于你的另一个问题,在 void 函数中调用 void 函数是可以的。但是您可能不想要void BossFight::getParty() { getParty(); },因为这会导致无限循环或堆栈溢出。
    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多