gpt4 book ai didi

从函数返回字符串的 C++ 错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:53:01 24 4
gpt4 key购买 nike

<分区>

我试图从函数 solution() 返回一个字符串,但我收到以下错误。抱歉,如果这是非常基本的,但任何人都可以解释如何返回字符串。我知道它与指针有关。

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;

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com