2

tried to open a .cpp in code::blocks. Got few lines of error

Partial code :

void QSort(string List[], int Left, int Right)
{
  int i, j;
  char *x;
  string TEMP;

  i = Left;
  j = Right;
  x = List[(Left+Right)/2];

  do {
    while((strcmp(List[i],x) < 0) && (i < Right)) {
       i++;
    }
    while((strcmp(List[j],x) > 0) && (j > Left)) {
        j--;
    }
    if(i <= j) {
      strcpy(TEMP, List[i]);
      strcpy(List[i], List[j]);
      strcpy(List[j], TEMP);
      i++;
      j--;
   }
  } while(i <= j);

  if(Left < j) {
     QSort(List, Left, j);
  }
  if(i < Right) {
     QSort(List, i, Right);
  }
}

I recieve this error in line

 x = List[(Left+Right)/2];

cannot convert 'std::string {aka std::basic_string}' to 'char*' in assignment|

2

1 Answer 1

3

Because they're incompatible. You need to call a member of std::string which returns a const char*.

x = List[(Left+Right)/2].c_str();

Be aware: this pointer is only valid for the life time of the std::string or until you modify the string object.

This function returns a const char* so you'll need to change the definition of x from char* to `const char*.

const char* x;

or better still, remove that line, and combine the two

void QSort(string List[], int Left, int Right)
{
    string TEMP;

    int i = Left;
    int j = Right;
    const char* x = List[(Left+Right)/2];

Infact, here's a rewrite that uses standard C++ algorithms throughout (std::string::compare instead of strcmp). This may make it easier for you to focus on the algorithm itself.

void QSort(string List[], int Left, int Right)
{
    int i = Left;
    int j = Right;
    const int mid = (Left+Right) / 2;

    for (;;) // repeat until we break.
    {
        // write both comparisons in terms of operator <
        while (List[i].compare(List[mid]) < 0 && i < Right)
            ++i;
        while (List[mid].compare(List[j]) < 0 && Left < j)
            --j;
        // if i == j then we reached an impasse.
        if (i >= j)
            break;
        std::swap(List[i], List[j]);
    }

  if(Left < j)
    QSort(List, Left, j);

  if(i < Right)
    QSort(List, i, Right);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I think is compiler issue, many lines of code affected.. any other solution?
It's not a compiler issue. You can't convert a std::string to a const char* because they are incompatible. You need to call the c_str() method of string to get the const char* which the std::string is managing.
Don't let the name, string, confuse you: std::string is a class name. char foo[] = "hello"; and char* foo = "hello"; are C-style strings, while std::string foo = "hello"; is a different beast entirely.
std::string::c_str() returns a const char* - the const indicating "you can't modify the thing the pointer points to". You just need to change x from char* x; to const char* x;. Infact, remove the char *x; line and make the change shown above.
Take a look at my cleanup of the algorithm which avoids using char*-related functions entirely and uses the std::string member functions to do the compares.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.