因此,我正在用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
}


但是我在第二个程序中看到错误,该第二个程序指出在构造函数'railsmentity :: railsmentity()'中,第二个错误显示错误:引用行printf(“%d \ n”的'['令牌之前的预期主表达式, railsmentity [1] [1]);

那么,我该怎么做才能使该变量可访问?

最佳答案

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

10-02 14:20