gpt4 book ai didi

c# - 类初始化错误

转载 作者:行者123 更新时间:2023-11-30 17:08:18 25 4
gpt4 key购买 nike

我正在使用 Visual Studio C# Express 和 Xna 4.0

好吧,我是一名业余程序员,我可能不是最好的方法,所以如果你愿意向我展示更好的方法并解决我的问题,那就太好了。

我的问题是我在运行这段代码时遇到了一个我无法理解的错误。

这是我正在使用的类,问题不在这里,但这很重要:

class ShipType
{
public Vector2 Size;
public int LogSlots;
public int DefSlots;
public int OffSlots;
public int SpecSlots;

public int HullBase;
public int TechCapacity;
public int EnergyCapacity;
public int WeightBase;
public string Class;
public string Manufacturer;
public string Description;

public void Initialize(
ref Vector2 Size,
ref int LogSlots, ref int DefSlots, ref int OffSlots,
ref int SpecSlots, ref int HullBase, ref int TechCapacity,
ref int EnergyCapacity, ref int WeightBase,
ref string Class, ref string Manufacturer, ref string Description)
{

}
}

这是我遇到问题的地方,运行初始化方法时出现错误:

class AOSEngine
{
public Player PlayerA = new Player();
public List<ShipType> ShipTypeList = new List<ShipType>(10);

public void Initialize()
{
//Build Ship Type List
ShipTypeList = new List<ShipType>(10);
ShipTypeList.Add(new ShipType());
ShipTypeList[0].Initialize(new Vector2(0, 0), 4, 4, 4, 4,
100, 100, 100, 10, "Cruiser",
"SpeedLight", "");

}
}

运行Initialize Line时出现错误,错误如下:

The Best Overloaded Method Match for AOS.ShipType.Initialize(ref
Vector2 Size, ... ref string Description)
has some invalid arguments.

再说一次,我可能做得不是很好,所以如果您有任何改进建议,我很想听听。

最佳答案

由于您已经将参数声明为ref,因此在传入变量时需要使用ref 关键字:

ShipTypeList[0].Initialize(ref new Vector2(0, 0),ref 4,ref 4,ref 4,ref 4,ref 100,ref 100,ref 100,ref 10,ref "Cruiser",ref "SpeedLight",ref "");

也就是说,没有充分的理由在这里使用 ref 参数 - 您最好从 Initlialize() 方法中删除 ref 关键字:

public void Initialize(
Vector2 Size,
int LogSlots, int DefSlots, int OffSlots, int SpecSlots,
int HullBase, int TechCapacity, int EnergyCapacity, int WeightBase,
string Class, string Manufacturer, string Description)
{
this.LogSlots = LogSlots;
this.DefSlots = DefSlots;
//...etc...
}

有关为什么以及如何使用 ref 的详细信息,请参阅 Jon Skeets 文章:http://www.yoda.arachsys.com/csharp/parameters.html

关于c# - 类初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13872521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com