Author | Message | Time |
---|---|---|
Brandon | Can someone please help me? I have been working on my programming assignment for my CS class and I have been running into a bunch of different problems. Some of which I have been able to over come (I hope), and others I haven't. One of these such problems is with the my_swap function that I created to help me sort an array. When I sent the array to the my_swap function, I get a few errors. At first, I have an infinite loop, but now I think I might have fixed that at the cost of getting two errors. With my little knowledge of C++ I haven't been able to figure out what they mean. So here they are: [quote] sortingalgs.cpp: In function `void bubbleSort(int*, int)': sortingalgs.cpp:19: error: invalid conversion from `int' to `int*' sortingalgs.cpp:19: error: invalid conversion from `int' to `int*' sortingalgs.cpp: In function `void bobSort(int*, int)': sortingalgs.cpp:42: error: invalid conversion from `int' to `int*' sortingalgs.cpp:42: error: invalid conversion from `int' to `int*' [/quote] Now here's the code: This is the file that contains the my_swap function: [code] #include "hw6.h" void my_swap(int first[], int second[], int index) { int temp = 0; // cout<<"In hw6.cpp in the my_swap function"; temp = first[index]; first[index] = second[index]; second[index] = temp; return; } [/code] The #include "hw6.h" is the header file where my prototype for the my_swap function is stored. Now for the sortingalgs.cpp: [code] #include "sortingalgs.h" #include "hw6.h" void bubbleSort(int array[], const int SIZE) { // cout<<"In sortingalgs.cpp in the bubbleSort"; bool done = false, swapped = true; do { swapped = false; for(int index = 0; index <= SIZE - 1; index++) { if(array[index] > array[index + 1]) { my_swap(array[index], array[index + 1], index); //swaps the values of the new indexes (Line 19) swapped = true; //Everytime it swaps, this is true. } } if(swapped == false) //Once swapping is over, this is true, and the for loop ends. done = true; //ends do-while loop. }while(done == false); return; } void bobSort(int unsorted_sub[], const int SIZE) { cout<<"In sortingalgs.cpp in the bobSort"; int sorted_sub[] = {}; //declares an array with an undetermined size filled with 0's (wrong?) for(int index = 0; index <= SIZE - 1; index++) { //Assigns the sorted array all of the unsorted array, so that it can be sorted sorted_sub[index] = unsorted_sub[index]; for(int index2 = 0; index2 <= index; index2++) { //Allows for two of the same arrays to exist so that the sorted can be sorted if(sorted_sub[index2] > sorted_sub[index]) my_swap(sorted_sub[index2], sorted_sub[index], index2); //line 42 } } return; } [/code] Can someone please help? Thanks! | October 16, 2006, 8:21 PM |
Final | Reverse the Declarations. void bubbleSort(int*, int); <--Yours void bubbleSort(int,int*);<--Mine | October 17, 2006, 5:46 AM |
Myndfyr | [quote author=Final link=topic=15882.msg159916#msg159916 date=1161063963] Reverse the Declarations. void bubbleSort(int*, int); <--Yours void bubbleSort(int,int*);<--Mine [/quote] Doesn't make much sense that he'd pass the array in without a pointer and the size of the array in by a pointer... | October 17, 2006, 8:47 AM |
Kp | [quote author=AntiVirus link=topic=15882.msg159884#msg159884 date=1161030065] Can someone please help me? I have been working on my programming assignment for my CS class and I have been running into a bunch of different problems. Some of which I have been able to over come (I hope), and others I haven't. One of these such problems is with the my_swap function that I created to help me sort an array. When I sent the array to the my_swap function, I get a few errors. At first, I have an infinite loop, but now I think I might have fixed that at the cost of getting two errors. With my little knowledge of C++ I haven't been able to figure out what they mean. So here they are: [quote] sortingalgs.cpp: In function `void bubbleSort(int*, int)': sortingalgs.cpp:19: error: invalid conversion from `int' to `int*' sortingalgs.cpp:19: error: invalid conversion from `int' to `int*' sortingalgs.cpp: In function `void bobSort(int*, int)': sortingalgs.cpp:42: error: invalid conversion from `int' to `int*' sortingalgs.cpp:42: error: invalid conversion from `int' to `int*' [/quote]Can someone please help? Thanks![/quote] You need another level of indirection on lines 19 and 42. You're trying to pass raw integers where the function calls for a pointer to an integer (as your error message stated)! Unless your assignment mandates the prototype for my_swap, I'd change it to void my_swap(int&, int&); (or template <typename T> void my_swap(T&, T&); if you're being fancy). | October 18, 2006, 3:44 AM |
Brandon | I could have been fancy and made it a template, but I decided to just add the my_swap function into my calling functions to avoid messing with pointers and that fixed it. Thanks! | October 18, 2006, 6:34 PM |
Final | ok one thing after the few years ive worked with c++ ive seen it really revolves around pointers mate so your going to have to learn them. Pointers are the essence of c++. TO ME. | October 19, 2006, 3:21 AM |
K | [quote author=Final link=topic=15882.msg160004#msg160004 date=1161228103] ok one thing after the few years ive worked with c++ ive seen it really revolves around pointers mate so your going to have to learn them. Pointers are the essence of c++. TO ME. [/quote] Pointers may be the essence of C. If you're writing C++, you should be using references in place of pointers in most cases. | October 19, 2006, 7:54 AM |
Final | ah i see | October 19, 2006, 1:34 PM |
Myndfyr | [quote author=K link=topic=15882.msg160009#msg160009 date=1161244488] [quote author=Final link=topic=15882.msg160004#msg160004 date=1161228103] ok one thing after the few years ive worked with c++ ive seen it really revolves around pointers mate so your going to have to learn them. Pointers are the essence of c++. TO ME. [/quote] Pointers may be the essence of C. If you're writing C++, you should be using references in place of pointers in most cases. [/quote] Still, references have some drawbacks, like that they can't be NULL, and that you can't reassign them. The non-NULL situation is a double-edged sword really -- it eliminates the need for NULL-pointer checking, but it limits their use in other situations. | October 19, 2006, 11:59 PM |
Final | wait i went stupid what is reference again im thinking this for some aparent reason & lol | October 20, 2006, 2:22 AM |
Kp | In general, use references for parameters which are mandatory and pointers for parameters which are optional. Then the caller can pass a NULL pointer for a parameter which is not supplied, but cannot pass NULL for parameters which are required. You may need to adjust this if you are in the habit of repointing arguments to local variables. I avoid that practice because it can confuse people who try to read the code later. | October 20, 2006, 4:43 AM |
Brandon | [quote author=Final link=topic=15882.msg160004#msg160004 date=1161228103] ok one thing after the few years ive worked with c++ ive seen it really revolves around pointers mate so your going to have to learn them. Pointers are the essence of c++. TO ME. [/quote] I don't really have a choice what I learn at school, unless I want to do research myself, and I have, but I don't have enough time to do extensive research and really learn it. I will either do that when I do have time, or just wait for it to show up in my class. Either way, for what I am doing, I shouldn't have to worry about them. | October 23, 2006, 5:08 PM |