尝试在C ++中的两个类文件之间使用全局2D数组

时间:2015-04-02 03:51:41

标签: c++ arrays multidimensional-array global-variables

所以我正在用C ++编写游戏,我需要在多个类文件之间全局共享2D数组。这是我的代码的更简单版本:

//创建和写入railsmentity [7] [100]

的值的第一个文件
#include "weaponfire.h"

#include <iostream>
#include <sdtlib.h>
#include <stdio.h>

using namespace std;

//Ship common variables
extern int shipgun;
extern int shipgunammo;
extern double shippower;

//Gun variables
extern int railsm[];
/*
//Projectile common variables
extern double gunx;
extern double guny;
extern double gunz;
extern double gunpitch;
extern double gunyaw;
extern double gunspeed;
extern double gunspeedmax;
*/
//Ship navigation variables
extern double x;
extern double y;
extern double z;
extern double pitch;
extern double yaw;
extern double truespeed;

//Entity variables
int extern railsmentitycount;
int railsmentity[7][100];//[0]existance, [1]x, [2]y, [3]z, [4]pitch, [5]yaw, [6]truespeed, [7]maxspeed
int railsmentitycountcurrent = 0;

extern bool space;
int looptemp = 0;

weaponfire::weaponfire()
{
    if(space == true){
        if(shipgun == 0){//No weapon
            printf("No weapon equipped!\n");
        }

        if(shipgun == 1){//railsm
            if(shipgunammo > 0 && shippower >= railsm[7]){//Check ammo and power
                shipgunammo--;//Decrease ammo
                shippower = shippower - railsm[7];//Drain power

                //Find a valid storage position
                while(looptemp <= 100){//If you are within the variable list bounds
                    if(railsmentity[0][looptemp] == 0){//If the entity slot is available
//                        railsmentitycountcurrent = railsmentitycount;
                        looptemp = 0;
                    }
                    else{//If entity slot is not available
                        if(looptemp > 100){
                            printf("Error\n");
                        }
                        looptemp++;
                    }
                }

                //railsm local initial entity raycast
                railsmentity[0][railsmentitycountcurrent] = 1;
                railsmentity[1][railsmentitycountcurrent] = x;
                railsmentity[2][railsmentitycountcurrent] = y;
                railsmentity[3][railsmentitycountcurrent] = z;
                railsmentity[4][railsmentitycountcurrent] = pitch;
                railsmentity[5][railsmentitycountcurrent] = yaw;
                railsmentity[6][railsmentitycountcurrent] = truespeed;
            }
        }
    }
}

weaponfire::~weaponfire()
{
    //dtor
}

//为了简单起见,这个其他类文件显示了railsmentity数组的特定值//

#include "railsmentity.h"
#include "weaponfire.h"

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

extern int railsmentity[7][100];//[0]existance, [1]x, [2]y, [3]z, [4]pitch, [5]yaw, [6]truespeed, [7]maxspeed

railsmentity::railsmentity()
{
    printf("%d\n", railsmentity[1][1]);
}

railsmentity::~railsmentity()
{
    //dtor
}

但是我在第二个程序中遇到错误,指出在构造函数'railmentity :: railsmentity()'中,第二个错误表示错误:在'''token <之前预期的primary-expression / em>引用 printf行(“%d \ n”,railsmentity [1] [1]);

那么我需要做些什么来使这个变量可以访问?

1 个答案:

答案 0 :(得分:1)

您有一个变量和一个名称相同的类railsmentity。这在C ++中不起作用,您需要将其中一个重命名为其他内容。