c++ - 从其他类文件 C++ 实现函数

标签 c++ function class

所以我对 C++ 有点陌生,我们的老师还没有告诉我们如何使用来自单独类文件的函数。

现在我们只是在做基于文本的东西,但我有它随机选择敌人类型和数组中的敌人。这是单独的类文件“enemy.cpp”中的函数

在“main.cpp”中我想调用这个函数。 (我认为它会像“enemy.genRandEnemy()”,但它不起作用)。

这是敌人类的代码 -

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

class enemy{
    public:
        void genRandEnemy();
};

void genRandEnemy() {

    string enemy[] = {"dragon", "troll", "wolf", "wraith", "spider", "scorpion", "hydra", "snake", "reaper", "centipede", "worm"};
    string enemyType[] = {"hell", "ice", "soul eater", "bone", "carnivorous"};

    srand(time(0)); 
    int randomEnemy = rand();
    int randomEnemyType = rand();

    int randEnemy = (randomEnemy % 11);
    int randEnemyType = (randomEnemyType % 5);

    if(randEnemyType == 0){cout << enemyType[0];} 
    else if(randEnemyType == 1){cout << enemyType[1];} 
    else if(randEnemyType == 2){cout << enemyType[2];}
    else if(randEnemyType == 3){cout << enemyType[3];}
    else if(randEnemyType == 4){cout << enemyType[4];}

    cout << " ";

    if(randEnemy == 0){cout << enemy[0];}
    if(randEnemy == 1){cout << enemy[1];}
    if(randEnemy == 2){cout << enemy[2];}
    if(randEnemy == 3){cout << enemy[3];}
    if(randEnemy == 4){cout << enemy[4];}
    if(randEnemy == 5){cout << enemy[5];}
    if(randEnemy == 6){cout << enemy[6];}
    if(randEnemy == 7){cout << enemy[7];}
    if(randEnemy == 8){cout << enemy[8];}
    if(randEnemy == 9){cout << enemy[9];}
    if(randEnemy == 10){cout << enemy[10];}
}

这是主类的代码片段 --

cout << "Then out comes three " << enemy.genRandEnemy() << "s and they encircle you. ";

同样在主类中,我在包含的地方找到了 -- #include "enemy.cpp"

我认为我没有正确设置敌人类来执行此操作,我也不确定如何设置。

最佳答案

不幸的是,您将代码拆分为多个文件。您应该将希望在多个文件中使用的类型和函数的声明放入一个头文件。因此,创建文件 enemy.hpp然后放enemy里面的声明:

class enemy
{
public:
  void genRandEnemy();
};

将函数的定义放到一个单独的源文件中并命名为enemy.cpp .它看起来像这样:

#include "enemy.hpp"
#include <iostream>
// All your other includes needed to define the function...

void
enemy::genRandEnemy()
{
  // Implementation of the function...
}

然后,在任何需要调用enemy::genRandEnemy的文件中:

#include "enemy.hpp"

void
f()
{
  enemy foe;
  foe.genRandEnemy();
  // Or whatever...
}

请注意您的代码不一致:您声明 genRandEnemy作为 enemy 类的成员函数它定义为任何类之外的自由函数。我的例子总是把它作为 enemy 的成员. (这实际上对我来说似乎很奇怪。具有该名称的函数应该创建一个新敌人并返回它。如果它是 enemy 的成员,我将需要一个 enemy 提前创建一个。)

另外两点:

  • 每次 genRandEnemy 时,您都在重新播种随机生成器函数被调用。这会导致随机性较差。
  • 我相信你能想出比一堆 if(randEnemy == 1) { cout << enemy[1]; } 更好的解决方案.可以吗?

关于c++ - 从其他类文件 C++ 实现函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25714745/

相关文章:

c++ - 如何在 vim 中默认折叠(折叠)Doxygen 注释?

VBA编写函数及返回值

c++ - C++ 的 libxml : How to add a root node to XML tree?

c++ - WinRT/Metro 风格应用程序中基于 ATL 的 COM 对象

python - 相同的函数在 Python 中以相反的顺序给出不同的结果。为什么?

c++ - 模板类成员函数的显式特化

c++ - 重载运算符以处理类对象?

c++ - 作为类成员的函数的定义

python - Python中的类是否在不同的文件中?

c++ - 在 Mac OS X 上使用 CUDA 5.5 时出现内核失败错误