不确定为什么会出现此错误。我有以下内容:
int* arr = new int[25];
int* foo(){
int* i;
cout << "Enter an integer:";
cin >> *i;
return i;
}
void test(int** myInt){
*myInt = foo();
}
This call here is where I get the error:
test(arr[0]); //here i get invalid conversion from int to int**
最佳答案
按照您编写的方式,test
接受一个指向 int
的指针,但是 arr[0]
只是一个 int
。
但是,在 foo
中,您提示输入一个 int
,但读入的位置是未初始化指针的值。我以为您希望 foo
读取并返回 int
。
例如
int foo() {
int i;
cout << "Enter an integer:";
cin >> i;
return i;
}
在这种情况下,测试采用指向 int 的指针(即 void test(int* myInt)
)是有意义的。
然后您可以将指针传递给您动态分配的 int
之一。
test(&arr[0]);
关于c++ - 从 int 到 int** C++ 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2677907/