我有一个类,它定义了几个全局变量,如下所示:
namespace Algo
{
public static class AlgorithmParameters
{
public int pop_size = 100;
}
}
在我的另一个 csharp 文件中,它也包含 main(),并且在 main() 中,我将类型结构的数组和数组大小声明为 pop_size,但我在"chromo_typ Population[AlgorithmParameters.pop_size];"
. 请在下面找到代码。我是否对可变长度大小的数组声明使用了不正确的语法?
namespace Algo
{
class Program
{
struct chromo_typ
{
string bits;
float fitness;
chromo_typ() {
bits = "";
fitness = 0.0f;
}
chromo_typ(string bts, float ftns)
{
bits = bts;
fitness = ftns;
}
};
static void Main(string[] args)
{
while (true)
{
chromo_typ Population[AlgorithmParameters.pop_size];
}
}
}
}
错误是:
Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
请帮忙。