By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,378 Members | 3,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,378 IT Pros & Developers. It's quick & easy.

Bubble Sort error

P: 3
Hey All,
I am getting an error when in my function that doesn't make sense.

Here is my function....

Expand|Select|Wrap|Line Numbers
  1. void sort(string strItem, int &arraySize)
  2. {
  3. int innerCounter = 0, outerCounter = 0;  //inner and outer loop counters
  4. string temp;
  5.  
  6. for(outerCounter = 0; outerCounter < arraySize - 1; outerCounter++)
  7.   for(innerCounter = 0; innerCounter < arraySize - 1 - outerCounter; innerCounter++)
  8.     if(strItem[innerCounter] > strItem[innerCounter + 1])
  9.     {
  10.         temp = strItem[innerCounter];
  11.         strItem[innerCounter] = strItem[innerCounter + 1];
  12.         strItem[innerCounter + 1] = temp;
  13.      }
  14.  
  15. return;
  16. }
The error I am getting is on the last line of the loop....
error C2440: '=' : cannot convert from 'std::string' to 'char'

If I move this code to main() it works fine.

Thanks in advance.
Aug 11 '07 #1
Share this Question
Share on Google+
5 Replies


ilikepython
Expert 100+
P: 844
Hey All,
I am getting an error when in my function that doesn't make sense.

Here is my function....

Expand|Select|Wrap|Line Numbers
  1. void sort(string strItem, int &arraySize)
  2. {
  3. int innerCounter = 0, outerCounter = 0;  //inner and outer loop counters
  4. string temp;
  5.  
  6. for(outerCounter = 0; outerCounter < arraySize - 1; outerCounter++)
  7.   for(innerCounter = 0; innerCounter < arraySize - 1 - outerCounter; innerCounter++)
  8.     if(strItem[innerCounter] > strItem[innerCounter + 1])
  9.     {
  10.         temp = strItem[innerCounter];
  11.         strItem[innerCounter] = strItem[innerCounter + 1];
  12.         strItem[innerCounter + 1] = temp;
  13.      }
  14.  
  15. return;
  16. }
The error I am getting is on the last line of the loop....
error C2440: '=' : cannot convert from 'std::string' to 'char'

If I move this code to main() it works fine.

Thanks in advance.
Make temp from string to a char so:
Expand|Select|Wrap|Line Numbers
  1. char temp;
  2.  
You are trying to asign a string to a char and that's why the compiler is giving you an error.
Aug 11 '07 #2

Expert 10K+
P: 11,448
Don't you want your strItem parameter to be an *array* of strings instead? You
wrote it was a string but the rest of your code indicates otherwise; or do you
want to sort the individual characters in a single string, e.g. "cba" --> "abc"?

kind regards,

Jos
Aug 12 '07 #3

P: 3
Thank You.....

As I mentioned, this is the exact code I had in main and it worked fine. So, I'm not sure why I wouldn't get the same compile error in main.

My data that I am trying to sort is an array of names that I am passing into the function.
Aug 12 '07 #4

Expert 10K+
P: 11,448
Thank You.....

As I mentioned, this is the exact code I had in main and it worked fine. So, I'm not sure why I wouldn't get the same compile error in main.

My data that I am trying to sort is an array of names that I am passing into the function.
I bet in your main function strItem is defined as follows:

Expand|Select|Wrap|Line Numbers
  1. string strItem[];
  2.  
Look closely how you defined your strItem parameter in your function.

kind regards,

Jos
Aug 12 '07 #5

P: 3
Ah ha....

here are my declarations in main

Expand|Select|Wrap|Line Numbers
  1. const int noOfItems = 25;
  2. string names[noOfItems];
and here is my prototype

Expand|Select|Wrap|Line Numbers
  1. void sort(string strItem, int &arraySize);
I see that my first parameter is a string and not an array of strings.
should the syntax of the first parameter be (string strItem[]) ?

C.
Aug 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.