cannot convert 'String::toInt' from type 'long int (String::)() const' to type 'int'
时间: 2023-12-19 17:04:34 浏览: 241
这个错误提示是因为你在将字符串转换为整数时使用了`String::toInt`函数,但是该函数返回的类型是`long int`,而你的目标类型是`int`,两者不一致导致编译器报错。
解决方法有两种:
1.将目标类型改为`long int`,即:
```
long int num = str.toInt();
```
2.将`String::toInt`函数返回的结果强制转换为`int`,即:
```
int num = static_cast<int>(str.toInt());
```
这样就可以将`long int`转换为`int`,解决编译器报错的问题。
相关问题
ambiguous overload for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'long long int')
This error message occurs when you try to assign a long long integer value to a std::string variable using the assignment operator (=). Since there is no overloaded operator= function that takes both a std::string and a long long integer argument, the compiler cannot determine which one to use.
To fix this error, you need to convert the long long integer to a std::string before assigning it to the variable. You can use std::to_string() function to convert the long long integer to a string as shown below:
```
std::string str = std::to_string(100LL); // convert long long integer to string
```
Alternatively, you can use a constructor of std::string that takes a const char* argument to create a string from a C-style string representation of the long long integer:
```
std::string str = std::string("100"); // create string from C-style string
```
In either case, the resulting string can be assigned to the std::string variable without any ambiguity.
阅读全文