2013-06-29 20 views
-3

正如你所看到的,一切都設置爲公共的,但是編譯器說:爲什麼我在此C#代碼中得到「...由於其保護級別而無法訪問」編譯錯誤?

Shooting.inventoryWeapon.inventoryWeapon(Shooting.weapon, int)無法訪問由於其保護級別。

此代碼在課堂上拍攝。

public enum weapon{gun,shotgun}; 

public struct inventoryWeapon{ 
    weapon current; 
    int shotAmmo; 
    inventoryWeapon(weapon cur,int shAmmo){ 
     current=cur; 
     shotAmmo=shAmmo; 
    } 
} 

public inventoryWeapon[] Inventory; 
int weaponIndex=0; 

void Start(){ 
    Inventory=new inventoryWeapon[10]; 
    Inventory[weaponIndex]= new inventoryWeapon(weapon.shotgun,30); 
} 
+0

的[什麼是公共,私有,保護,並沒有什麼區別?(可能重複http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-私人保護和沒有) –

回答

2

您需要添加public這裏:

public inventoryWeapon(weapon cur,int shAmmo){ 
    current=cur; 
    shotAmmo=shAmmo; 
} 
+0

它的工作,謝謝你:) – user2534306

0

即使您已經定義類作爲公共

public struct inventoryWeapon{ 

如果你想你的類成員暴露在你外面的世界將你的班級成員定義爲公開的(除非你使用了無用性)。

public inventoryWeapon(weapon cur,int shAmmo){ 
相關問題