C# strange problem with constructor
I have IMHO very strange problem with three-argument constructor , When I try to run the program visual studio s开发者_开发技巧hows me only one bug: " 'Sort.HeapSort' does not contain a constructor that takes 3 arguments 112 35" .
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
}
}
}
Your constructor is declared private, since you omitted an access specifier. Add the public
keyword prior to the constructor definition.
You need to specify your constructor as public, thusly:
public HeapSort(int size, int min1, int max1)
The compiler allows you to omit the accessibility specifier and defaults to private. Bit of a syntactic sugar gotcha IMO that I wish would be done away with.
So, since you have a private constructor, your client code does not "see" it and the compiler tries to call the public constructor which naturally results in the error you're seeing.
Your constructor isn't public, it's private (you didn't include any modifiers so it defaults to private) therefore the calling code can't "see" it.
Change:
HeapSort(int size, int min1, int max1)
To:
public HeapSort(int size, int min1, int max1)
You need to add public
to your constructor, otherwise it's considered private
, and therefore inaccessible from within your Main()
.
The constructor with three params has no accessibility modifier on it and so defaults to private
.
Change the declaration to public HeapSort(int size, int min1, int max1)
and you'll be good-to-go.
It looks like you are missing 'public' keyword before constructor.
Make your constructor public !
Your constructor is private. It needs to be public.
精彩评论