无效的操作数可转换为二进制表达式(' rgbtriple' and' int€#x27;)

发布于 2025-02-01 01:40:26 字数 2033 浏览 3 评论 0原文

当我尝试查看特定值是否存在时,我会遇到此错误。我使用了CS50为我提供的自定义数据类型,称为rgbtriple。如果该值不存在在内存中价值。因此,我使用了一个称为isnull的函数。它将检查该值是否存在在内存中。如果不是零,则将返回1,否则为0。

这是我的代码

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // entering the array of the image
    for (int i = 0; i < height; i++)
    {
        // entering the array of the row
        for (int j = 0; j < width; j++)
        {
            /* blur: the element we select we need to get the value
               of its neighbour value and adding this all including it
               then get the avg value of pixel that need to set
             */

            int element = 0;
            int blueSum = 0;
            int greenSum = 0;
            int redSum = 0;

            RGBTRIPLE pixels[] = {
                image[i][j], image[i][j - 1], image[i][j + 1],
                image[i - 1][j - 1], image[i - 1][j], image[i - 1][i + 1],
                image[i + 1][j - 1], image[i + 1][j], image[i + 1][j + 1]
            };

            for (int k = 0; k < 9; k++)
            {
                if (isNull(pixels[k]) == 1)
                {
                    element++;
                    blueSum += pixels[k].rgbtBlue;
                    greenSum += pixels[k].rgbtGreen;
                    redSum += pixels[k].rgbtRed;
                }
            }

            image[i][j].rgbtBlue = round(blueSum / element);
            image[i][j].rgbtGreen = round(greenSum / element);
            image[i][j].rgbtRed = round(redSum / element);
        }
    }
    return;
}

// check whether it is null or not
int isNull(RGBTRIPLE pixel)
{
    if (pixel != 0)
    {
        return 1;
    }

    return 0;
}

错误我得到了:

$ make filter
helpers.c:142:15: error: invalid operands to binary expression ('RGBTRIPLE' and 'int')
    if (pixel != 0)
        ~~~~~ ^  ~
1 error generated.
make: *** [Makefile:2: filter] Error 1

I got this error when I tried to look particular value exist or not. I used a custom data type called RGBTRIPLE that cs50 provides me. If the value is not exist in the memory I will get 'segmentation fault' because I need to find out top left, top middle, top right, value that I have its previous value and next value, bottom left, bottom middle, bottom right value. So for that I used a function called isNull. It will check whether the value exists in the memory or not. If it is not null It will return 1, otherwise 0.

Here is my code

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // entering the array of the image
    for (int i = 0; i < height; i++)
    {
        // entering the array of the row
        for (int j = 0; j < width; j++)
        {
            /* blur: the element we select we need to get the value
               of its neighbour value and adding this all including it
               then get the avg value of pixel that need to set
             */

            int element = 0;
            int blueSum = 0;
            int greenSum = 0;
            int redSum = 0;

            RGBTRIPLE pixels[] = {
                image[i][j], image[i][j - 1], image[i][j + 1],
                image[i - 1][j - 1], image[i - 1][j], image[i - 1][i + 1],
                image[i + 1][j - 1], image[i + 1][j], image[i + 1][j + 1]
            };

            for (int k = 0; k < 9; k++)
            {
                if (isNull(pixels[k]) == 1)
                {
                    element++;
                    blueSum += pixels[k].rgbtBlue;
                    greenSum += pixels[k].rgbtGreen;
                    redSum += pixels[k].rgbtRed;
                }
            }

            image[i][j].rgbtBlue = round(blueSum / element);
            image[i][j].rgbtGreen = round(greenSum / element);
            image[i][j].rgbtRed = round(redSum / element);
        }
    }
    return;
}

// check whether it is null or not
int isNull(RGBTRIPLE pixel)
{
    if (pixel != 0)
    {
        return 1;
    }

    return 0;
}

Error I got:

$ make filter
helpers.c:142:15: error: invalid operands to binary expression ('RGBTRIPLE' and 'int')
    if (pixel != 0)
        ~~~~~ ^  ~
1 error generated.
make: *** [Makefile:2: filter] Error 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

类型rgbtriple是在 bmp.h 中定义的:

/**
 * RGBTRIPLE
 *
 * This structure describes a color consisting of relative intensities of
 * red, green, and blue.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
 */
typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

它是一个结构:您不能将结构与==操作员进行比较,您必须比较会员单独。

问题是:您是什么意思,请检查它是否为 是什么?

“像素是黑色的?

// check whether it is black or not
int isBlack(RGBTRIPLE pixel) {
    return ((pixel.rgbtBlue | pixel.rgbtGreen | pixel.rgbtRed) == 0);
}

如果您的意思是 >:

  • 第六个初始化器映像[i -1] [i + 1]有一个错字,
  • 您必须为图像边界制作特殊情况(i == 0,<代码> j == 0 ,<代码> i ==高度 - 1 和j == width -1)。

这是一个简单的修复:

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a < b ? b : a; }
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // entering the array of the image
    for (int i = 0; i < height; i++) {
        // entering the array of the row
        for (int j = 0; j < width; j++) {
            /* blur: compute the new color by averaging the components
               of all pixels in a 3x3 area around the pixel.
               assume that pixel colors continue beyond the image
               borders.
             */
            unsigned blueSum = 0;
            unsigned greenSum = 0;
            unsigned redSum = 0;
            int i1 = max(0, i - 1);
            int i2 = min(height - 1, i + 1);
            int j1 = max(0, j - 1);
            int j2 = min(width - 1, j + 1);

            RGBTRIPLE pixels[] = {
                image[i][j1], image[i][j], image[i][j2],
                image[i1][j1], image[i1][j], image[i1][j2],
                image[i2][j1], image[i2][j], image[i2][j2]
            };

            for (int k = 0; k < 9; k++) {
                blueSum += pixels[k].rgbtBlue;
                greenSum += pixels[k].rgbtGreen;
                redSum += pixels[k].rgbtRed;
            }
            image[i][j].rgbtBlue = round(blueSum / 9);
            image[i][j].rgbtGreen = round(greenSum / 9);
            image[i][j].rgbtRed = round(redSum / 9);
        }
    }
}

但是,请注意,上面的函数无法按编码工作,因为它覆盖将用于下一列和下一行的像素值。要执行此转换,您可以使用3行缓冲区来保持先前的值。

这是一个修改版本:

typedef unsigned char BYTE;

typedef struct {
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
} RGBTRIPLE;

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE row[3][width + 2];

    // populate the initial the row cache
    row[1][0] = row[2][0] = image[0][0];
    row[1][width + 1] = row[2][width + 1] = image[0][width - 1];
    for (int j = 0; j < width; j++) {
        row[1][j + 1] = row[2][j + 1] = image[0][j];
    }

    // entering the array of the image
    for (int i = 0; i < height; i++) {
        // update the row cache
        for (int j = 0; j < width + 2; j++) {
            row[0][j] = row[1][j];
            row[1][j] = row[2][j];
        }
        if (i < height - 2) {
            row[2][0] = image[i + 1][0];
            row[2][width + 1] = image[i + 1][width - 1];
            for (int j = 0; j < width; j++)
                row[2][j + 1] = image[i + 1][j + 1];
        }
        // update the image row
        for (int j = 0; j < width; j++) {
            /* blur: compute the new color by averaging the components
               of all pixel values in a 3x3 area around the pixel.
               Assume that pixel colors are extended beyond the image
               borders.
             */
            unsigned blueSum  = 0;
            unsigned greenSum = 0;
            unsigned redSum   = 0;

            for (int ii = 0; ii < 3; ii++) {
                for (int jj = j; jj < j + 3; jj++) {
                    blueSum  += row[ii][jj].rgbtBlue;
                    greenSum += row[ii][jj].rgbtGreen;
                    redSum   += row[ii][jj].rgbtRed;
                }
            }
            image[i][j].rgbtBlue  = (BYTE)((blueSum + 4)  / 9);
            image[i][j].rgbtGreen = (BYTE)((greenSum + 4) / 9);
            image[i][j].rgbtRed   = (BYTE)((redSum + 4)   / 9);
        }
    }
}

the type RGBTRIPLE is defined in bmp.h as:

/**
 * RGBTRIPLE
 *
 * This structure describes a color consisting of relative intensities of
 * red, green, and blue.
 *
 * Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
 */
typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

It is a structure: you cannot compare structures with the == operator, you must compare members individually.

The problem is: what do you mean by check whether it is null or not?

If you mean "is the pixel black?", you should test if all 3 components are 0:

// check whether it is black or not
int isBlack(RGBTRIPLE pixel) {
    return ((pixel.rgbtBlue | pixel.rgbtGreen | pixel.rgbtRed) == 0);
}

You get a segmentation fault because you read pixels beyond the boundaries of the matrix image:

  • the 6th initializer image[i - 1][i + 1] has a typo
  • you must make special cases for the image boundaries (i == 0, j == 0, i == height - 1 and j == width - 1).

Here is a simple fix:

int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a < b ? b : a; }
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // entering the array of the image
    for (int i = 0; i < height; i++) {
        // entering the array of the row
        for (int j = 0; j < width; j++) {
            /* blur: compute the new color by averaging the components
               of all pixels in a 3x3 area around the pixel.
               assume that pixel colors continue beyond the image
               borders.
             */
            unsigned blueSum = 0;
            unsigned greenSum = 0;
            unsigned redSum = 0;
            int i1 = max(0, i - 1);
            int i2 = min(height - 1, i + 1);
            int j1 = max(0, j - 1);
            int j2 = min(width - 1, j + 1);

            RGBTRIPLE pixels[] = {
                image[i][j1], image[i][j], image[i][j2],
                image[i1][j1], image[i1][j], image[i1][j2],
                image[i2][j1], image[i2][j], image[i2][j2]
            };

            for (int k = 0; k < 9; k++) {
                blueSum += pixels[k].rgbtBlue;
                greenSum += pixels[k].rgbtGreen;
                redSum += pixels[k].rgbtRed;
            }
            image[i][j].rgbtBlue = round(blueSum / 9);
            image[i][j].rgbtGreen = round(greenSum / 9);
            image[i][j].rgbtRed = round(redSum / 9);
        }
    }
}

Note however that the above function cannot work as coded because it overwrites the pixel values that will be used for the next column and for the next row. To perform this transformation in place, you can use a 3 line buffer to keep the previous values.

Here is a modified version:

typedef unsigned char BYTE;

typedef struct {
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
} RGBTRIPLE;

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE row[3][width + 2];

    // populate the initial the row cache
    row[1][0] = row[2][0] = image[0][0];
    row[1][width + 1] = row[2][width + 1] = image[0][width - 1];
    for (int j = 0; j < width; j++) {
        row[1][j + 1] = row[2][j + 1] = image[0][j];
    }

    // entering the array of the image
    for (int i = 0; i < height; i++) {
        // update the row cache
        for (int j = 0; j < width + 2; j++) {
            row[0][j] = row[1][j];
            row[1][j] = row[2][j];
        }
        if (i < height - 2) {
            row[2][0] = image[i + 1][0];
            row[2][width + 1] = image[i + 1][width - 1];
            for (int j = 0; j < width; j++)
                row[2][j + 1] = image[i + 1][j + 1];
        }
        // update the image row
        for (int j = 0; j < width; j++) {
            /* blur: compute the new color by averaging the components
               of all pixel values in a 3x3 area around the pixel.
               Assume that pixel colors are extended beyond the image
               borders.
             */
            unsigned blueSum  = 0;
            unsigned greenSum = 0;
            unsigned redSum   = 0;

            for (int ii = 0; ii < 3; ii++) {
                for (int jj = j; jj < j + 3; jj++) {
                    blueSum  += row[ii][jj].rgbtBlue;
                    greenSum += row[ii][jj].rgbtGreen;
                    redSum   += row[ii][jj].rgbtRed;
                }
            }
            image[i][j].rgbtBlue  = (BYTE)((blueSum + 4)  / 9);
            image[i][j].rgbtGreen = (BYTE)((greenSum + 4) / 9);
            image[i][j].rgbtRed   = (BYTE)((redSum + 4)   / 9);
        }
    }
}
三生池水覆流年 2025-02-08 01:40:26

您不能将标量类型与结构或数组进行比较。这就是错误消息告诉您的。

将多个无关字节的结构与数值进行比较应该是什么条件?
那不是它的工作方式。您只能分别比较字段或将整个结构与同一结构类型的另一个变量进行比较。

除此之外,您的代码中有一些误解。

如果该值不存在在内存中,则我会得到“分割故障”

您会得到一个分段故障,因为要读取的内存不是您的内存。您没有访问特权可以阅读或写作。

它将检查该值是否存在在内存中。

如果您为函数提供一些价值,它将始终“存在于内存中”。您已经在调用功能中提供了一个值。怎么不存在?
您无法通过查看复制值来检测复制的内存位置是否有效。


您的根本问题是,您没有在阅读值之前之前与数组的界限进行验证。
您必须将行和列索引与限制进行比较,并且仅在不超出界限时才访问数组元素。

您需要重新设计方法以收集像素以模糊。

You cannot compare scalar types with a struct or an array. That is what the error message is telling you.

What condition should be true to make a struct of multiple unrelated bytes to compare with a numerical value?
That's not how it works. You can only compare the fields separately or compare whole struct with another variable of same struct type.

Besides that, you have a few misconceptions in your code.

If the value is not exist in the memory I will get 'segmentation fault'

You got a segmentation fault because the memory you want to read is not your memory. You have no access privileges to read or write.

It will check whether the value exists in the memory or not.

If you provide some value to a function, it will always "exist in memory". You have provided a value in the calling function. How would it not exist?
You cannot detect if the memory location where you copied it from was valid by just looking at the copied value.


Your underlying problem is that you do not verify that you are withing bounds of your array before reading the values.
You must compare row and column index with the limits and only access the array element if you are not out of bounds.

You need to rework your approach to collect pixels to blur.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文