int getHammingDistance(shot1,shot2)初始化:无法从“initializer list”转换为“int”
时间: 2023-12-27 18:05:35 浏览: 38
这个错误通常是因为函数参数的类型没有指定,导致编译器无法正确识别参数类型。在这个函数中,shot1和shot2的类型应该明确定义为整型,例如:
```cpp
int getHammingDistance(int shot1, int shot2) {
// 函数体
}
```
你也可以在函数声明中指定参数类型,例如:
```cpp
int getHammingDistance(int, int);
```
然后在函数定义中提供具体的参数名称和类型。无论哪种方法,都需要明确指定函数参数的类型。
相关问题
元素“1”: 从“initializer list”转换到“QPointF”需要收缩转换
这个错误提示的意思是在初始化列表中将一种类型的值转换为另一种类型时,可能会丢失精度或范围。在这种情况下,您正在将一个值转换为QPointF类型,但是该值可能超出了QPointF类型的范围或精度。要解决此问题,您可以尝试使用更精确的类型或重新设计代码以避免需要此类型的转换。如果您确定此转换是安全的,则可以使用显式类型转换来进行转换,但是请注意潜在的精度问题。
c++ 中使用initializer_list初始化对象必须使用隐式转换码
不是必须使用隐式转换。在使用 initializer_list 初始化对象时,可以使用显式转换或者类定义的构造函数进行初始化。例如:
```c++
#include <initializer_list>
#include <iostream>
#include <string>
class MyVector {
public:
MyVector(std::initializer_list<int> list) {
for (auto i : list) {
values.push_back(i);
}
}
void print() {
for (auto v : values) {
std::cout << v << " ";
}
std::cout << std::endl;
}
private:
std::vector<int> values;
};
int main() {
MyVector v1{1, 2, 3}; // 使用 initializer_list 初始化对象
v1.print();
MyVector v2 = {4, 5, 6}; // 使用 initializer_list 初始化对象
v2.print();
MyVector v3 = std::initializer_list<int>{7, 8, 9}; // 使用显式转换初始化对象
v3.print();
MyVector v4(std::initializer_list<int>{10, 11, 12}); // 使用构造函数初始化对象
v4.print();
return 0;
}
```
在上面的例子中,我们使用 initializer_list 初始化了四个不同的 MyVector 对象,分别使用了隐式转换、显示转换和构造函数进行初始化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)