将数组作为函数参数传递时出错

时间:2021-08-28 05:27:54

I am getting error when I pass an array to a function

我将数组传递给函数时遇到错误

Array: int red_eachpix[MAX_PIX] = { 0 }

数组:int red_eachpix [MAX_PIX] = {0}

Function:

void sum_individualpix(int *pixels, int pixle) {
    pixels[(pixle - 1)]++;
}

Error: error C2664: 'void sum_individualpix(int,int)' : cannot convert argument 1 from 'int [255]' to 'int'

错误:错误C2664:'void sum_individualpix(int,int)':无法将参数1从'int [255]'转换为'int'

The whole program is given below:

整个计划如下:

#define MAX_PIX 255
#define WIDTH 25
#define HEIGHT 25

void sum_individualpix(int , int);    // I think the error is here

int main()
{
    int X, Y, red, counter = 0;
    int red_eachpix[MAX_PIX] = { 0 }, Red[WIDTH][HEIGHT] = { 0 };
    long sum_red = 0;
    in.open("basicval.txt");
    if (in)
    {
        in >> X >> Y >> red;  //Data is in form of:   X  Y    B1(red pixel value)
        while (!in.eof())
            {
                counter++;
                sum_red += red;

                Red[X][Y] = red;

                sum_individualpix(red_eachpix, red);    //Getting Error here

                in >> X >> Y >> red;
             }

         double avg_red = (double)sum_red / counter;
         cout << "Average Value for Red = " << avg_red << endl;
    }
    in.close();
    getchar();
}

void sum_individualpix(int *pixels, int pixle) {
    pixels[(pixle - 1)]++;
}

Just in case anyone wants to know the program reads the pixel values of an image, while the image itself has been converted into ASCII values by a program

以防万一有人想知道程序读取图像的像素值,而图像本身已被程序转换为ASCII值

Edited: Actually the Red[WIDTH][HEIGHT] & int X, Y, red, counter = 0; was not a mistake. I wrote it wrong accidentally.

编辑:实际上红色[宽度] [高度]&int X,Y,红色,计数器= 0;这不是一个错误。我不小心写错了。

3 个解决方案

#1


1  

You defined two times the same name 'red':

你定义了两次相同名称'red':

int X, Y, red, counter = 0;
int red_eachpix[MAX_PIX] = { 0 }, red[WIDTH][HEIGHT] = { 0 };

#2


1  

you have int X, Y, red, counter = 0;, an int variable called red
also red[WIDTH][HEIGHT] = { 0 }; int array called red

你有int X,Y,red,counter = 0 ;,一个名为red的int变量也是red [WIDTH] [HEIGHT] = {0}; int数组称为红色

#3


0  

void sum_individualpix(int *pixels, int pixle) {
    pixels[(pixle - 1)]++; //Shows that first argument type is array
}

The error is with the function declaration void sum_individualpix(int , int) the first (argument) data type specifier is actually an array (int[]) not an integer(int) so the declaration should be changed to void sum_individualpix(int[], int)

错误是函数声明void sum_individualpix(int,int)第一个(参数)数据类型说明符实际上是一个数组(int [])而不是整数(int)所以声明应该更改为void sum_individualpix(int [] ,int)

#1


1  

You defined two times the same name 'red':

你定义了两次相同名称'red':

int X, Y, red, counter = 0;
int red_eachpix[MAX_PIX] = { 0 }, red[WIDTH][HEIGHT] = { 0 };

#2


1  

you have int X, Y, red, counter = 0;, an int variable called red
also red[WIDTH][HEIGHT] = { 0 }; int array called red

你有int X,Y,red,counter = 0 ;,一个名为red的int变量也是red [WIDTH] [HEIGHT] = {0}; int数组称为红色

#3


0  

void sum_individualpix(int *pixels, int pixle) {
    pixels[(pixle - 1)]++; //Shows that first argument type is array
}

The error is with the function declaration void sum_individualpix(int , int) the first (argument) data type specifier is actually an array (int[]) not an integer(int) so the declaration should be changed to void sum_individualpix(int[], int)

错误是函数声明void sum_individualpix(int,int)第一个(参数)数据类型说明符实际上是一个数组(int [])而不是整数(int)所以声明应该更改为void sum_individualpix(int [] ,int)