请教一个较搞人的问题?

dreamblood 2002-04-19 05:09:36
class Point3d
{
public Point3d(int a ; int b){...}
}

class B
{
public Point3d[] p=new Point3d[10];
}

结果出错:error cs0052 Inconsistent accessibility: field type '_3Dbox.Point3d[]' is less accessible than field '_3Dbox.Cubic.p'


...全文
165 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
iyiduhsoad 2002-04-20
  • 打赏
  • 举报
回复
我认为你的问题是没有给
p[0],p[1],p[2]....p[10]赋初值。
就你写出的程序来说,在class B初始化的时候,p被初始为
一个class Point3d的数组。但p[0],p[1]没有被初始。
如我修改的程序,是可以运行的。
using System;

namespace formytest
{
class Point3d
{
public int A;
public int B;
public Point3d(int a,int b)
{
A=a;
B=b;
}
}

class B
{

public Point3d[] p=new Point3d[10];
public B()
{
for(int i=0;i<10;i++)
p[i]=new Point3d(i,i);
}
}
class test
{
public static void Main()
{
int i=0;
B b=new B();
Console.WriteLine(b.p[3].A);
}
}


}
norzss 2002-04-20
  • 打赏
  • 举报
回复
显然在构造数组对象p的时候Point3d没有对应的构造函数可以调用:
和c++一样,当你不提供构造函数时候,编译器会为你自动生成一个不带参数的
构造函数:Point3d{},而此时你提供了构造函数,编译器就不会为你自动生成构造函数了,所以你必须自己写一个不带参数的构造函数,才能保证:
public Point3d[] p=new Point3d[10];
有相应的构造函数可以调用,ok?
snewxf 2002-04-20
  • 打赏
  • 举报
回复
class Point3d
{
public Point3d(int a ; int b){...}//老大里面是,逗号。******
}

class B
{
static void Main()
{
public Point3d[] p=new Point3d[10];
for(int i = 0 ;i<10;i++)
{
p[1] = new Point3d(i,i+1);
}
}
}

qqchen79 2002-04-20
  • 打赏
  • 举报
回复
BTW: 你的源程序里B应该是public的吧?否则也不会有问题。
qqchen79 2002-04-20
  • 打赏
  • 举报
回复
更本没有调用过constructor!生成的数组只是hold reference,还没有对象生成呢。错误更本就不再这里。

你的Point3d class是internal的(by default),但p是public的,违反了Accessibility Restriction,所以编译不通过。
可以把Point3d改成public的,或者把p的定义改成internal的。
wanliguocsdn 2002-04-19
  • 打赏
  • 举报
回复
public Point3d[] p=new Point3d[10];//[]应该是()吧
调用的是缺省构造函数,而这里没给出来
Meditate 2002-04-19
  • 打赏
  • 举报
回复
应该是虚名+疯之驴的答案
wenzm 2002-04-19
  • 打赏
  • 举报
回复
不是说了不一致吗
你的构造函数定义了两个参数,而你调用时切只传入了一个,当然不可以了

public Point3d(int a ; int b){...}

public Point3d[] p=new Point3d[10];//[]应该是()吧
xming076 2002-04-19
  • 打赏
  • 举报
回复
class Point3d
{
public Point3d(int a ; int b){...}
// ^这里应该是逗号
}
klxyz 2002-04-19
  • 打赏
  • 举报
回复
估计是初始化数据比字段数少的问题,如果不对 别见笑

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧