using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
int function(int n)
{
int j = 2;
for (int i = 1; i <= n; i++)
j = j << 2;
return j; }
static void Main(string[] args)
{
int i = 2;
function(i);
}
}
}
Error 1 An object reference is required for the nonstatic field, method, or property 'ConsoleApplication1.Program.function(int)' D:\程序\77\ConsoleApplication1\ConsoleApplication1\Program.cs 28 13 ConsoleApplication1这个是什么毛病呢?谢谢指教!
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
int function(int n)
{
int j = 2;
for (int i = 1; i <= n; i++)
j = j << 2;
return j; }
static void Main(string[] args)
{
int i = 2;
function(i);
}
}
}
Error 1 An object reference is required for the nonstatic field, method, or property 'ConsoleApplication1.Program.function(int)' D:\程序\77\ConsoleApplication1\ConsoleApplication1\Program.cs 28 13 ConsoleApplication1这个是什么毛病呢?谢谢指教!
static int function(int n)
就对了,
或者不该函数声明,把调用的地方改一下
Program pp = new Program();
pp.function(i);
这样就不会出错了
原因是引用类的非静态成员必须将类实例化,引用类的静态成员时只需要在成员名称前加上类名称就可以,你这个例子因为在这个类内部引用函数,所以可以将类名称省略.