2

I'm trying to return a string from the function solution() but I am getting the error below. Apologies if this is pretty basic but could anybody explain how to return the string. I understand that it is related to pointers.

error: could not convert ‘(std::__cxx11::string*)(& hexaDeciNum)’ from ‘std::__cxx11::string* {aka std::__cxx11::basic_string*}’ to ‘std::__cxx11::string {aka std::__cxx11::basic_string}’

string solution(string &S){

    int n = stoi(S);
    int answer = 0;

    // char array to store hexadecimal number
    string hexaDeciNum[100];

    // counter for hexadecimal number array
    int i = 0;
    while(n!=0)
    {
        // temporary variable to store remainder
        int temp  = 0;

        // storing remainder in temp variable.
        temp = n % 16;

        // check if temp < 10
        if(temp < 10)
        {
            hexaDeciNum[i] = temp + 48;
            i++;
        }
        else
        {
            hexaDeciNum[i] = temp + 55;
            i++;
        }

        n = n/16;
    }

    // printing hexadecimal number array in reverse order
    for(int j=i-1; j>=0; j--){
        //cout << hexaDeciNum[j] << "\n";

    if (hexaDeciNum[j].compare("A") ==0 or hexaDeciNum[j].compare("B") ==0 or hexaDeciNum[j].compare("C") ==0 or hexaDeciNum[j].compare("D") ==0 or hexaDeciNum[j].compare("E") ==0 or hexaDeciNum[j].compare("F") ==0 or hexaDeciNum[j].compare("1") ==0 or hexaDeciNum[j].compare("0") ==0 ) {
        answer = 1;
    }


}
    if (answer == 1){
        return hexaDeciNum;
    }
    else {
        return "ERROR";
    }
}


int main() {

    string word = "257";

    string answer = solution(word);

    return 0;

}
lotto 1988
  • 33
  • 1
  • 3
  • There is already an answer to a similar question, hope this helps: https://stackoverflow.com/questions/40297274/error-could-not-convert-from-stdstring-aka-stdbasic-stringchar-to – Erindy May 02 '18 at 10:44
  • 1
    A typo? The comment says you want a char array, but you made an array of string: `string hexaDeciNum[100];`. – HolyBlackCat May 02 '18 at 10:48
  • 1
    You are trying to return string array which decays to a pointer to the first string element, hence the conversion error. You could make a string and += all strings from array to it then return it – Killzone Kid May 02 '18 at 10:51

2 Answers2

4

hexaDeciNum is defined as string hexaDeciNum[100]. It is not a string - it is an array of 100 string instances.

You're attempting to return it from a function that should return string.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
0

You should define hexaDeciNum as string hexaDeciNum; instead of string hexaDeciNum[100];. With that way, you can still indexing operator. However you can not use compare method anymore, because each element of string is a char. Instead use operator == like in the following for your piece of code.

// printing hexadecimal number array in reverse order
    for(int j=i-1; j>=0; j--){
        //cout << hexaDeciNum[j] << "\n";

        if (hexaDeciNum[j] == 'A' or hexaDeciNum[j]=='B' or 
            hexaDeciNum[j] == 'C' or hexaDeciNum[j] == 'D' or 
            hexaDeciNum[j] == 'E' or hexaDeciNum[j] == 'F' or 
            hexaDeciNum[j] == '1' or hexaDeciNum[j] == '0' ) {
            answer = 1;
        }
    }

and please don't forget to compile it for c++ 11 with -std=c++11 option of compiler.

eneski
  • 1,575
  • 17
  • 40