简体   繁体   English

在C ++中从'char'到'const Char *'的无效转换

[英]Invalid conversion from 'char' to 'const Char*' in C++

I have declared a function (C++) 我已经声明了一个函数(C ++)

int products(char num1, char num2, char num3, char num4, char num5);

but my compiler is giving me this error: 但是我的编译器给我这个错误:

Invalid conversion from 'char' to 'const Char*'
error initializing argument 1 of 'int atoi (const char*)'  on line 22

when I attempt to pass num1 , num2 etc. as a parameter to atoi . 当我尝试将num1num2等作为参数传递给atoi

#include<iostream>
#include<string>

using namespace std;

int Product(char num1, char num2, char num3, char num4, char num5);

const string LargeNum = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";

int main(){
int greatestVal = 0;
for(int i = 0; i + 4 <= LargeNum.length(); i++ ){
    if(greatestVal < Product(LargeNum[i], LargeNum[i+1], LargeNum[i+2], LargeNum[i+3], LargeNum[i+4])){
        greatestVal = Product(LargeNum[i], LargeNum[i+1], LargeNum[i+2], LargeNum[i+3], LargeNum[i+4]);
    }
}
cout << greatestVal << endl;
system("PAUSE");
}

int Product(char num1, char num2, char num3, char num4, char num5){
    return (atoi(num1)*atoi(num2)*atoi(num3)*atoi(num4)*atoi(num5));
}

atoi takes a const char* (ie a null terminated sequence of characters). atoi采用const char* (即,以空终止的字符序列)。 You are supplying it with a single char , so the compiler complains. 您为它提供了一个char ,因此编译器抱怨。 How you fix it depends on exactly what you are trying to do. 您如何修复它取决于您要做什么。

I would guess that you want to convert a char representing a digit to an integer, '0' to 0, '1' to 1 etc. If so then the correct code would be 我猜想您想将代表数字的char转换为整数,'0'转换为0,'1'转换为1等等。如果是这样,那么正确的代码将是

return (num1 - '0')*(num2 - '0')*(num3 - '0')*(num4 - '0')*(num5 - '0');

This works because chars convert to ints automatically when you do arithmetic on them, and also because the chars '0' to '9' are guaranteed to be in sequence, so all you need to do to convert a digit to an int is subtract '0' from it. 之所以可行,是因为当您对chars进行算术运算时chars会自动将其转换为ints,并且因为chars'0'至'9'被保证是按顺序排列的,因此将数字转换为int所要做的就是减去' 0'。

Please see the documentation of atoi first. 请首先查看atoi的文档。 It takes const char* as argument, while you're passing char to it. 当您将char传递给char ,它将const char*作为参数。 That is definitely an error. 那绝对是一个错误。

Also, you don't need atoi . 另外,您不需要atoi If you want to convert into '7' into 7 , then just substract '0' from it. 如果要将'7'转换为7 ,则只需将其减去'0' So what you probably want is this: 因此,您可能想要的是:

int Product(char num1, char num2, char num3, char num4, char num5)
{
    return (num1-'0')*(num2-'0')*(num3-'0')*(num4-'0')*(num5-'0');
}

Function atoi expects parameter of type const char* (string), you're passing single character to it. 函数atoi期望使用const char* (字符串)类型的参数,您正在向其传递单个字符。

If you want to retrieve integer value from a character containing single digit, you can use '0' character. 如果要从包含一位数字的字符中检索整数值,则可以使用'0'字符。

atoi expects a const char* as parameter, and you're giving it a char . atoi期望使用const char*作为参数,而您给它一个char The error message is pretty clear. 错误消息很清楚。

To convert a char to an int , you can do num1 - '0' : 要将char转换为int ,可以执行num1 - '0'

int Product(char num1, char num2, char num3, char num4, char num5){
   return (num1-'0')*(num2-'0')*(num3-'0')*(num4-'0')*(num5-'0');
}

The reason why you are getting a compilation error is because there is no matching overload for the set of arguments you are passing to the method. 收到编译错误的原因是,传递给方法的参数集没有匹配的重载。 The compiler tries to find the closest match, which in your case char and const char*, and then reports that error. 编译器尝试查找最接近的匹配(在您的情况下为char和const char *),然后报告该错误。

Note that atoi doesn't work because atoi takes in a string and not a single char -- for the very good reason that most numbers have more than one digit! 请注意atoi不起作用,因为atoi接受一个字符串而不是一个字符 -出于很好的理由,大多数数字都超过一个数字!

You can do this: 你可以这样做:

int Product(char num1, char num2, char num3, char num4, char num5){
    return (num1 - '0')*(num2 - '0')*(num3 - '0')*(num4 - '0')*(num5 - '0');
}

instead. 代替。

As many people are saying you can just use char - '0' to convert a char to an integer. 正如许多人说的那样,您可以只使用char-'0'将char转换为整数。 This is because in ASCII 0-9 are represented by the numbers 48-57 so if you take away '0' which is equivalent to 48 it will leave you with the result between 0-9 as long as you pass in a number 0 to 9. 这是因为在ASCII 0-9中用数字48-57表示,因此,如果您减去等于48的“ 0”,则只要将数字0传递给0,就将使结果保持在0-9之间9。

Just be careful you don't loop past the end of LargeNum as you are currently looping to the end of the string and accessing 3 past it. 请注意,不要循环到LargeNum的末尾,因为您当前正在循环到字符串的末尾并访问3。 You may want to loop to LargeNum.length - 3; 您可能需要循环到LargeNum.length-3;。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++从&#39;char *&#39;到&#39;const unsigned char *&#39;的无效转换 - C++ invalid conversion from ‘char*’ to ‘const unsigned char*’ (C++) 错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive] - (C++) error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] C ++从&#39;const char *&#39;到&#39;char *&#39;的无效转换 - c++ invalid conversion from 'const char*' to 'char*' C ++从&#39;char&#39;到&#39;const char&#39;的无效转换 - C++ invalid conversion from 'char' to 'const char' C ++从&#39;const char *&#39;到&#39;char *&#39;Arduino Uno的无效转换 - C++ invalid conversion from 'const char*' to 'char*' Arduino Uno C ++错误:从'char'到'const char *'的转换无效 - C++ Error: Invalid conversion from 'char' to 'const char*' c ++“错误:从&#39;const char *&#39;到&#39;char *&#39;的无效转换” - c++ “error: invalid conversion from ‘const char*’ to ‘char*’” C ++,从&#39;char&#39;到&#39;const char *&#39;的转换无效 - C++, Invalid conversion from ‘char’ to ‘const char*’ 从 'char' 到 'const char*' 的无效转换 c++ - invalid conversion from ‘char’ to ‘const char*’ c++ C ++从&#39;char&#39;到&#39;const char *&#39;的无效转换[-fpermissive] - C++ invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM