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



所以我正在用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("Errorn");
                        }
                        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("%dn", railsmentity[1][1]);
}
railsmentity::~railsmentity()
{
    //dtor
}

但我在第二个程序中遇到了错误,该程序在构造函数'railsmentity::railsmentity()'中声明,第二个错误说错误:引用行printf("%d\n",railsmentiy[1][1])的'[]标记之前应为主表达式

那么,我需要做些什么才能访问这个变量呢?

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

相关内容

  • 没有找到相关文章

最新更新