【问题标题】:C# function error (object reference)C#函数错误(对象引用)
【发布时间】:2012-05-16 09:27:40
【问题描述】:

非常直截了当的问题,在 Visual C# Express 中,当我在 main 中调用 Romberg 函数时出现了一个我不知道如何解决的错误(我在相应的行提供了错误消息)。我也尝试过使用 this.Romberg,但没有帮助。我应该怎么做?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Romberg2
{
    class test
    {
        // the function
        double f(double x)
        {
            double f;
            f=8*(Math.Sqrt(1-x*x)-x);
            return f;
        }

        // The Integration function
        void Romberg(double a, double b, int n, double[,] R)
        {
            int i, j, k;
            double h, sum;

            h = b - a;
            R[0,0]=(h/2)*(f(a)+f(b));

            for (i=1; i<=n; i++)
            {
                h=h/2;
                sum=0;

                for (k=1; k <= (Math.Pow(2.0,i)-1); k +=2)
                    sum +=f(a+k*h);

                R[i,0]=R[i-1,0]/2+sum*h;

                for(j=1; j<=i; j++)
                    R[i,j]=R[i,j-1]+(R[i,j-1]-R[i-1,j-1])/(Math.Pow(4.0,j)-1);
            }
        }

        static void main(int argc, char [] argv)
        {
            double[,] R = new double [10,10];
            int n=5, digits=13;
            double a=0, b=1/Math.Sqrt(2.0);

            // Call the integrating function
            Romberg(a, b, n, R);//ERROR: An object reference is required for the non-static field, method, or property

            // Printout the diagram
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<=i; j++)
                {
                    Console.WriteLine(R[i,j] + " ");
                }
                Console.WriteLine("\n");
            }

        }
    }
}

【问题讨论】:

  • 添加静态到 void Romberg -> static void Romberg
  • @Marc 最后我也知道了答案:(

标签: c# function object reference


【解决方案1】:

Romberg 不是静态方法,所以你应该使用 new 关键字通过实例调用它

new test().Romberg(a, b, n, R);

或者干脆把它变成一个静态函数

static void Romberg(double a, double b, int n, double[,] R)

【讨论】:

    【解决方案2】:

    main 是静态的,所以它不能调用实例方法。将Romberg 声明为静态,或创建test 的实例并使用该实例:

    var inst = new test();
    inst.Romberg(a, b, n, R);
    

    更多信息,请查看Static Classes and Static Class Members.上的MSDN文章

    【讨论】:

      【解决方案3】:

      Main 是静态方法,Roomberg 是实例方法。要么在 Main 中创建类的新实例并通过实例调用它,要么也将 Roomberg 设为静态。

      【讨论】:

        【解决方案4】:

        三个问题:

        1) Main 格式不正确。它需要采用以下格式之一:

        static void Main() {...} 
        static void Main(string[] args) {... } 
        static int Main() {...} 
        static int Main(string [] args) {...}
        

        只需将其更改为'static void Main()'

        2) 将Romberg方法设为静态

        static void Romberg(double a, double b, int n, double[,] R)
        

        3) 将 f 方法设为静态:

        static double f(double x)
        

        【讨论】:

          【解决方案5】:

          您需要将您的 Romberg 函数设为静态,或者您可以将它包装在它自己的类中,实例化并执行它。

          【讨论】:

            【解决方案6】:

            您需要在实例上调用该方法。该方法属于该类的实例。

            var inst = new test();
            inst.Romberg(q, w, e, R);
            

            【讨论】:

              【解决方案7】:

              试试这个。要调用Romberg(a, b, n, R);,您必须首先创建test 类对象。 Romberg 是一个实例方法。

              test obj=new test();
              obj.Romberg(a, b, n, R);
              

              【讨论】:

                【解决方案8】:

                您正在尝试从未初始化的类中调用方法。

                 static void Romberg(double a, double b, int n, double[,] R)
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-12-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多