【问题标题】:Structures in C#C# 中的结构
【发布时间】:2012-05-03 12:37:30
【问题描述】:

我在我的程序中使用如下结构:

public struct chromo_typ
{
        public string   bits;  
        public float    fitness;

        chromo_typ(string bts, float ftns)
        {
            bits = bts;
            fitness = ftns;
        }
};

我正在使用结构中定义的构造函数,即 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

我在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?)

我是否使用了不正确的结构?我该如何解决这个问题?

【问题讨论】:

  • 您忘记使用 new 关键字。构造函数不是方法。
  • 您的意思是temp[cPop++] = new chromo_typ(offspring1, 0.0f); 吗?
  • 尝试使用'new'关键字,即我的构造函数调用语法现在是“temp[cPop++] = new chromo_typ(offspring1, 0.0f);”但我现在收到此错误“'VM_Placement.Program.chromo_typ' 不包含采用 2 个参数的构造函数”
  • 但我确实有一个构造函数在我的结构中采用 2 个参数...

标签: c# struct


【解决方案1】:

在这种情况下,chromo_typ 是一个构造函数,但您将其作为实例方法调用。构造函数用于构造对象,即,

temp[cPop++] = new chromo_typ(arg1, arg2);

这不是您可以在您的类型的实例上调用的方法。

顺便说一句,在 C# 中命名类型的规范方法是使用以大写字母开头并使用驼峰式大小写,即

public struct ChromoTyp { }

当然,这不是一个规则,但遵循社区已经在使用的模式通常是个好主意。

【讨论】:

  • 是的,我知道它是一个构造函数,但是如何调用该构造函数呢?我需要创建结构对象吗?
  • @user1277070:我给你举了一个例子。您必须使用 new 关键字。
  • 嘿,我尝试在您的示例中以相同的方式使用“new”关键字...但现在我收到此错误:“VM_Placement.Program.chromo_typ”不包含带有 2 个参数的构造函数"
  • 我的语法是“temp[cPop++] = new chromo_typ(offspring1, 0.0f);”
  • @EdS。看起来他的构造函数是私有的,不是吗?
猜你喜欢
  • 1970-01-01
  • 2011-07-18
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多