2010-11-17 176 views
1

我有三个参数构造函数的恕我直言很奇怪的问题,当我尝试运行程序时,Visual Studio只显示了一个错误:“'Sort.HeapSort'不包含构造函数,它需要3个参数112 35“。C#构造函数的奇怪问题

namespace Sort 
{ 
    class HeapSort 
    { 
     private int[] A; 
     private int heapSize; 
     private int min; 
     private int max; 
     Random myRandom = new Random(); 

     HeapSort(int size, int min1, int max1) //this is the three argument constructor. 
     { 
      heapSize = size - 1; 
      min = min1; 
      max = max1; 
      A = new int[size]; 
     }  
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      int size = 30; 
      int min = 0; 
      int max = 100; 

      HeapSort myHeapSort = new HeapSort(size,min,max); //In this line is the bug 
     } 
    } 
} 
+1

你有没有尝试过公开构造函数? – 2010-11-17 16:36:06

+0

你能删除无用的线路吗?:) – ykatchou 2010-11-17 16:36:21

+0

也许你应该用适当的访问修饰符来修饰你的类。公共,私人,保护等。 – StingyJack 2010-11-17 16:36:37

回答

9

由于您省略了访问说明符,因此您的构造函数被声明为private。在构造函数定义之前添加public关键字。

+0

它的工作原理,谢谢:) – Konrad 2010-11-17 16:42:09

+0

你应该检查更新/向下按钮下面的方框接受他的答案,而不是编辑标题来标记它已解决。 – 2010-11-17 17:12:57

3

你需要指定你的构造为public,正是如此:

public HeapSort(int size, int min1, int max1) 

编译器允许你省略无障碍符,默认为私有。国际海事组织的一句话,我希望会被废除。

因此,由于您有一个私有构造函数,因此您的客户端代码不会“看见”它,编译器会尝试调用公共构造函数,这会自然导致您看到的错误。

+0

它的作品谢谢:) – Konrad 2010-11-17 16:42:33

2

你的构造函数不公开,它是私有的(你没有包含任何修饰符,所以它默认为私有的),因此调用代码不能“看见”它。

变化:

HeapSort(int size, int min1, int max1) 

要:

public HeapSort(int size, int min1, int max1) 
1

您需要添加public到你的构造,否则它被认为是private,因此从Main()内无法访问。

1

带有三个参数的构造函数没有辅助功能修饰符,因此默认为private

将声明更改为public HeapSort(int size, int min1, int max1),那么您将很适合。

0

看起来你在构造函数之前缺少'public'关键字。

0

让您的构造函数公开!

0

您的构造函数是私有的。它需要公开。