简体   繁体   English

错误:从“int”到“int*”的无效转换

[英]error : invalid conversion from 'int' to 'int*'

I'm getting the following error in my code:我的代码中出现以下错误:

error: invalid conversion from 'int' to 'int*'错误:从“int”到“int*”的无效转换

I have made a function printArray() , and when in my main body I am just using printArray() to come up with output.我已经制作了 function printArray printArray() ,而在我的主体中,我只是使用printArray()来提出 output。 It works perfectly fine.它工作得很好。

But when I write it like:但是当我这样写时:

cout << "The array after swapping values will be " << printArray(arr, 6) << endl;

It shows that error.它显示了该错误。

#include<iostream>
using namespace std;

void reverse(int arr[] , int n)
{
    int start =0 ;
    int end= n-1;

    while ( start<=end){
        swap( arr[start] , arr[end] );
        start++ ;
        end-- ;
    }
}

void printArray(int arr[] , int n){
    for ( int i=0 ; i<n ; i++ ){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}


int main(){
    int arr[6]= { 1 , 2 , 3 , 4 , 5 , 6 };
    reverse(arr, 6);
    cout<< "The array after swapping values will be " <<printArray(arr, 6)<<endl;
    //printArray(arr, 6);

    return 0;
}

I just commented out the printArray(arr,6) line to get my output using cout , but it is showing the error.我刚刚注释掉了printArray(arr,6)行以使用cout获取我的 output ,但它显示了错误。

It's my first ever question asked to StackOverflow, so I might not be able to explain it perfectly to you, and I am sorry for that.这是我向 StackOverflow 提出的第一个问题,所以我可能无法完美地向您解释,对此我深表歉意。 Please do help me with it.请帮帮我。

The error message is confusing.错误消息令人困惑。

Nevertheless the function printArray has the return type void That is the function returns nothing.然而 function printArray具有返回类型void即 function 什么也不返回。

void printArray(int arr[] , int n){

So this statement所以这个说法

cout<< "The array after swapping values will be " <<printArray(arr, 6)<<endl;

is incorrect.是不正确的。

You could split it like你可以像这样拆分它

cout<< "The array after swapping values will be ";
printArray(arr, 6);
cout<<endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM