• Welcome to Valhalla Legends Archive.
 

I don't know why I have these errors! [C++]

Started by AntiVirus, October 16, 2006, 03:21 PM

Previous topic - Next topic

AntiVirus

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*'

Now here's the code:

This is the file that contains the my_swap function:




#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;
}


The #include "hw6.h" is the header file where my prototype for the my_swap function is stored.

Now for the sortingalgs.cpp:


#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;
}


Can someone please help?  Thanks!
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Final

#1
Reverse the Declarations.

void bubbleSort(int*, int); <--Yours

void bubbleSort(int,int*);<--Mine

MyndFyre

Quote from: Final on October 17, 2006, 12:46 AM
Reverse the Declarations.

void bubbleSort(int*, int); <--Yours

void bubbleSort(int,int*);<--Mine
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...
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Kp

Quote from: AntiVirus on October 16, 2006, 03:21 PM
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*'
Can someone please help?  Thanks!

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).
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

AntiVirus

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!
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

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.

K

Quote from: Final on October 18, 2006, 10:21 PM
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.

Pointers may be the essence of C.  If you're writing C++, you should be using references in place of pointers in most cases.


MyndFyre

Quote from: K on October 19, 2006, 02:54 AM
Quote from: Final on October 18, 2006, 10:21 PM
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.

Pointers may be the essence of C.  If you're writing C++, you should be using references in place of pointers in most cases.

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Final

wait i went stupid what is reference again im thinking this for some aparent reason

& lol

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

AntiVirus

Quote from: Final on October 18, 2006, 10:21 PM
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.
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.
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus