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

时间:2014-09-07 21:00:48

标签: 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&#34; enemy.cpp&#34;

我不认为我的敌人班级设置正确,我不确定如何。

2 个答案:

答案 0 :(得分:1)

将代码拆分为多个文件是不幸的。您应该将要在多个文件中使用的类型和函数的声明放入头文件。因此,创建文件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成员函数,但 define 它作为任何课程之外的自由功能。我的例子总是把它作为enemy的成员。 (这实际上似乎对我很熟。具有该名称的函数应该创建一个新的敌人并且返回它。如果它是enemy的成员,我需要提前enemy创建一个。)

另外两条评论:

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

答案 1 :(得分:0)

这是你的班级:

class Enemy{ //I change it to not confuse with the local variable in genRandEnemy.
    public:
        void genRandEnemy(); //you should return a string instead of the printing
};

函数原型是:

void Enemy::genRandEnemy() {
...
}

你可以这样使用它:

int main()
{
   Enemy e;//declare a variable
   //now you use the variable:
   cout << "Then out comes three ";
   e.genRandEnemy();
   cout << "s and they encircle you. ";
   return 0;
}

顺便说一句:

你可以缩短你的代码:

 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<<enemyType[randEnemyType];     

同样的:

 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 << enemy[randEnemy];