![]() |
| ||
| Problem converting Selection Sort to Class hi everybody, this is the small c++ program for selection sort using functions. #include <iostream> and i would like to change the same selection sort program using class, and following is the source code for selection sort with class. #include <iostream> unfortunately i got compilation errors, i have no idea how to correct them:rolleyes: can someone help me to find out is there any syntax errors for the second source code? i really appreciate your help.... |
| ||
| Re: Problem converting Selection Sort to Class Your problem is that you don't have a constructor for the Selectionsort class which accepts the values you're trying to initialise it with. You should try a constructor which accepts a range of values - eg, Selectionsort::Selectionsort(int* begin, int* end) or one which accepts an array, eg, Selectionsort::Selectionsort(int arr[]) Whichever of these you use, I don't think you can use the initialisation syntax as it stands in main() - you would probably need to declare the array seperately first before passing it to the Selectionsort constructor: int main() A couple of other comments - you don't need to pass the array size to the function, you can work it out using ( sizeof(numbers) / sizeof(int) )Or you can use a template trick to deduce the size of the array.. template<int N> |
| ||
| Re: Problem converting Selection Sort to Class Actually.. you don't need to pass the array or the size at all - because both data members are stored in your class. You probably want a blank parameter list for the sort function... And since you can work out the size of the array, you shouldn't need to store it as a data member either, you could create a member function to work it out int Selectionsort::size() |
| ||
| Re: Problem converting Selection Sort to Class You may be experiencing ambiguity. int numbers[] and array_size passed to sort() as parameters are the same names as member variables. How is the compiler to know which to use? Also, the member variable int numbers[] is never allocated any memory, yet you try to use it in print(). Try commenting out the member variables numbers[] and array_size and the print() function and recompile. I have to wonder why you want to put this in a class at all, but if that's your goal, then you have two choices. You can either use an array member variable and copy data from the array you want to sort into the member variable array and sort the member variable array, or you can remove the member array variable, pass an array, and use it as is, passing it from method to method as needed. If you decide to use a member array variable then you need to allocate memory for it. You can either use static memory to hard code the size at compilation time or you can use dynamic memory to allow run time sizing. |
| All times are GMT -4. The time now is 1:54 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC