Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法将参数“1”的“int*”转换为“float*”_C++_Arrays_C++11_Pointers - Fatal编程技术网

C++ 无法将参数“1”的“int*”转换为“float*”

C++ 无法将参数“1”的“int*”转换为“float*”,c++,arrays,c++11,pointers,C++,Arrays,C++11,Pointers,我正在使用指针将和整数数组复制到浮点数组中 // Copy This code into main.cpp file #include <iostream> using namespace std; int main() { int array1[5]; int array2[7]; float array3[5]; int x; cout << "Enter 5 values :" << endl;

我正在使用指针将和整数数组复制到浮点数组中

// Copy This code into main.cpp file

#include <iostream>

using namespace std;

int main()

{

    int array1[5];

    int array2[7];

    float array3[5];

    int x;

    cout << "Enter 5 values :" << endl;
    for ( int i = 0; i < 5; i++ )
        {
            cin >> array1[i];
        }

        cout << "Enter 7 values :" << endl;
        for ( int j = 0; j < 7; j++ )
        {
            cin >> array2[j];
        }

    cout << "Please choose an option from the list : " << endl
     << "\t 1. integer array copy " << endl
     << "\t 2. float array copy " << endl
     << "\t 3. delete single row of array " << endl
     << "\t 4. delete block of  rows into array " << endl
     << "\t 5. add a row into the array " << endl
     << "\t 6. add block of rows into array " << endl
     << "\t 7. Quit " << endl;

    cin >> x;

    if ( x == 1 )

    {

        int *z = arrayCopy ( array1, 5, array2, 7 );

        for ( int i = 0; i < 12; i++){

            cout << z[i] << " ";

         }

        cout << endl ;

    }

    else if ( x == 2 )

    {

        float *z = floatArrayCopy ( array1, 5, array2, 7 );

        for ( int i = 0; i < 12; i++){

            cout << z[i] << " ";

         }

        cout << endl;

    }

    return 0;

}


// And copy this code into "arrayUtil.h" file

int *arrayCopy( int *a, int size1, int *b, int size2)

{

    int *c = new int[size1 + size2];

    for ( int i = 0; i < size1; i++)

    {

        c[i] = a[i];

    }

    for ( int i = 0; i < size2; i++)

    {
        c[size1 + i] = b[i];
    }

    return c;
}

float *floatArrayCopy( float *a, int size1, float *b, int size2)

{

    float *c = new float[size1 + size2];

    for ( int i = 0; i < size1; i++)

    {

        c[i] = a[i];

    }

    for ( int i = 0; i < size2; i++)

    {
        c[size1 + i] = b[i];

    }

    return c;

}
这是一个错误。。我更改了参数值和指针类型,但仍然给出了问题。。需要知道为什么


提前感谢..:

问题出在电话上

其中,array1和array2是int的数组,因此将转换为指向int的指针,即int*与函数定义相对

传递指针时,指针类型必须匹配-浮点*和int*不匹配

一个C++编译器会诊断出这个错误,并给出一个错误信息来反映上述。


本例中的解决方案是将浮点数组传递给函数,而不是int数组。

在floatArrayCopy array1、5、array2、7中,将int数组传递给需要浮点数组的函数。错误信息似乎非常清楚。虽然您通常可以隐式地将int值转换为float值,反之亦然,int值数组与float值数组非常不同。我更改了类型,但仍然会出现错误,我认为您没有将其更改为:float*floatArrayCopy int*a,int size1,int*b,int size2,尽管在这种情况下它的名称可能会错误。您的更改没有多大意义。您没有2个浮点数组作为参数。你不会只想返回一个浮点数。你不想要一个浮点数作为大小,你不想这样做。您希望像我上面所说的那样传递int数组并返回一个浮点数组。不通过浮点数组。再次重命名该函数,使其有意义。我知道使用for循环将int数组中的每个值存储到float的方法,但我不知道如何使用指针动态执行。。你的回答真令人满意。。现在我如何将int数组作为float传递给参数中的floatArrayFunction???@MuhammadZeeshan只要照我说的做就行了。虽然我会更改它的名称:类似于这个float*copyandConvertIntArrayStofOlarAyint*ar1,int-size1,int*ar2,int-size2@drescherjm成功了。。谢谢。。
float *z = floatArrayCopy ( array1, 5, array2, 7 );
float *floatArrayCopy( float *a, int size1, float *b, int size2);