我在我的程序中使用了一个结构如下:
public struct chromo_typ
{
public string bits;
public float fitness;
chromo_typ(string bts, float ftns)
{
bits = bts;
fitness = ftns;
}
};
Run Code Online (Sandbox Code Playgroud)
我正在使用结构中定义的构造函数,即我的main()中的chromo_typ(string bts,float ftns).我的main()包含以下代码:
chromo_typ[] temp = new chromo_typ[VM_Placement.AlgorithmParameters.pop_size];
chromo_typ ct = new chromo_typ();
int cPop = 0;
//loop until we have created POP_SIZE new chromosomes
while (cPop < VM_Placement.AlgorithmParameters.pop_size)
{
// we are going to create the new population by grabbing members of the old population
// two at a time via roulette wheel selection.
string offspring1 = p.Roulette(TotalFitness, Population);
string offspring2 = p.Roulette(TotalFitness, Population);
//add crossover dependent on the crossover rate
p.Crossover(offspring1, offspring2);
//now mutate dependent on the mutation rate
p.Mutate(offspring1);
p.Mutate(offspring2);
//add these offspring to the new population. (assigning zero as their
//fitness scores)
temp[cPop++] = ct.chromo_typ(offspring1, 0.0f);
temp[cPop++] = ct.chromo_typ(offspring2, 0.0f);
}//end loop
Run Code Online (Sandbox Code Playgroud)
我收到以下错误temp[cPop++] = ct.chromo_typ(offspring1, 0.0f);和temp[cPop++] = ct.chromo_typ(offspring2, 0.0f);
错误:
'VM_Placement.Program.chromo_typ' does not contain a definition for 'chromo_typ' and no extension method 'chromo_typ' accepting a first argument of type 'VM_Placement.Program.chromo_typ' could be found (are you missing a using directive or an assembly reference?)
我使用结构不正确吗?我该如何解决这个问题?
在这种情况下,chromo_typ是构造函数,但您将其作为实例方法调用.构造函数用于构造对象,即
temp[cPop++] = new chromo_typ(arg1, arg2);
Run Code Online (Sandbox Code Playgroud)
它不是一种可以调用类型实例的方法.
在旁注中,在C#中命名类型的规范方法是使用大写字母开头并使用驼峰大小写,即,
public struct ChromoTyp { }
Run Code Online (Sandbox Code Playgroud)
当然,这不是一个规则,但遵循社区已经使用的模式通常是一个好主意.
| 归档时间: |
|
| 查看次数: |
244 次 |
| 最近记录: |