类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
argument of type "int*" is incompatible with parameter of type "int**" error in C++
我的教授要求我创建函数并使用指针 为每个函数传递一个整数数组作为参数,但是当我插入他创建的测试代码时,我得到类型"int*"的参数与类型"int**"的参数不兼容错误。
#include <iostream>
#include "homework.h"
using namespace std;
int main() {
int a[10] = { 3, 5, 6, 8, 12, 13, 16, 17, 18, 20 };
int b[6] = { 18, 16, 19, 3 ,14, 6 };
int c[5] = { 5, 2, 4, 3, 1 };
Homework h;
// testing initialize_array
h.print_array(a, 10); // ERROR
h.initialize_array(a, 10); // ERROR
h.print_array(a, 10); // ERROR
h.print_array(b, 6); // ERROR
h.selection_sort(b, 6); // ERROR
h.print_array(b, 6); // ERROR
cout << "Factorial of 5 = " << h.factorial (5) <<endl; //print: 120
c[0] = h.factorial(c[0]);
c[1] = h.factorial(c[2]);
h.print_array(c, 5); // ERROR
return 0;
})
以下是家庭作业的源文件:
#include <iostream>
#include "homework.h"
using namespace std;
void Homework::initialize_array(int *numArr[], int size)
{
for (int count = 0; count < size; count++) {
// if divisible by 2, replace the value to 0.
if (count % 2 == 0) {
*numArr[count] = 0;
}
else {
*numArr[count] = 1;
}
}
}
void Homework::print_array(int *numArr[], int size)
{
// prints each values in the array.
for (int count = 0; count < size; count++) {
if (count == 0) {
cout << *numArr[count];
}
else {
cout << ", " << *numArr[count];
}
}
cout << endl;
}
void Homework::selection_sort(int *numArr[], int size)
{
int i, j, minIndex;
// determine the minimum value.
for (i = size - 1; i > 0; i--) {
minIndex = 0;
for (j=1; j<=i; j++) {
if (*numArr[j] < *numArr[minIndex])
minIndex = j;
}
// swap values.
int temp = *numArr[minIndex];
*numArr[minIndex] = *numArr[i];
*numArr[i] = temp;
}
}
int Homework::factorial(int num)
{
if (num == 0 || num == 1)
return 1;
else
// if not 0 or 1, recall the function.
return(num * factorial(num - 1));
}
如果你的教授想要显示指针,你可以这样做:
void Homework::print_array(int *numArr, int size)//exact same as int numArr[] as it decays to a pointer
{
for (int count = 0; count < size; count++) {
if (count == 0) {
cout << numArr[count];
}
else {
cout << ", " << numArr[count];
}
}
cout << endl;
}
int main()
{
int a[10] = { 3, 5, 6, 8, 12, 13, 16, 17, 18, 20 };
Homework h;
h.print_array(a, 10);
return 0;
}
编辑:在想也许你的教授希望你演示一系列这样的指针:
void Homework::print_array(int *numArr[], int size)
{
for (int count = 0; count < size; count++) {
if (count == 0) {
cout << *numArr[count];
}
else {
cout << ", " << *numArr[count];
}
}
cout << endl;
}
int main()
{
int a[10] = { 3, 5, 6, 8, 12, 13, 16, 17, 18, 20 };
int* arrayPtr[10];
for(int i=0; i<10; i++){
arrayPtr[i] = &a[i];
}
Homework h;
h.print_array(arrayPtr, 10);
return 0;
}
你不应该有一个指针数组。你应该有一个整数数组:int *numArr[]
void Homework::print_array(int numArr[], int size) //remove the extra * on front
{
// prints each values in the array.
for (int count = 0; count < size; count++) {
if (count == 0) {
cout << numArr[count];
}
else {
cout << ", " << numArr[count];
}
}
cout << endl;
}
好吧,如果您必须使用int *numArr[]
,请保留原始print_array功能,但是如果您尝试打印它,请使用h.print_array(&a, 10);
.
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- C++ 模板:重载时找不到基类类型参数方法