c - 将 type int 乘以 struct 时出错

标签 c cs50

下面的代码在尝试编译时出现以下错误:

二进制表达式的无效操作数(“int”和“RGBTRIPLE”)

基本上,我不知道如何正确地将 temp[i][j] 乘以一个因数。

例如,我尝试过像 2 * int(temp[i][j]) 这样的类型转换,但这不起作用。我该怎么做呢?在这种情况下,我需要进一步了解类型的哪些方面?

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j].rgbtRed   = image[i][j].rgbtRed;
            temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
            temp[i][j].rgbtBlue  = image[i][j].rgbtBlue;
        }
    }

    int red_gx, green_gx, blue_gx, red_gy, green_gy, blue_gy;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // Top Left Corner
            if (i == 0 && j == 0)
            {
                red_gx   = 0;
                green_gx = 0;
                blue_gx  = 0;

                red_gy   = 0;
                green_gy = 0;
                blue_gy  = 0;
            }

            //
            // <-- some other elif statements here in between
            //

            // Any other pixel
            else
            {
                red_gx   = (-1 * (temp[i - 1][j - 1])) + temp[i - 1][j + 1] + (-2 * (temp[i][j - 1])) + (2 * (temp[i][j + 1])) + (-1 * (temp[i + 1][j - 1])) + (temp[i + 1][j + 1]);
            //     green_gx = (-1 * temp[i - 1][j - 1]) + temp[i - 1][j + 1] + (-2 * temp[i][j - 1]) + (2 * temp[i][j + 1]) + (-1 * temp[i + 1][j - 1]) + temp[i + 1][j + 1];
            //     blue_gx  = (-1 * temp[i - 1][j - 1]) + temp[i - 1][j + 1] + (-2 * temp[i][j - 1]) + (2 * temp[i][j + 1]) + (-1 * temp[i + 1][j - 1]) + temp[i + 1][j + 1];

            //     red_gy   = (-1 * temp[i - 1][j - 1]) + (-2 * temp[i - 1][j]) + (-1 * temp[i - 1][j + 1]) + temp[i + 1][j - 1] + (2 * temp[i + 1][j]) + temp[i + 1][j + 1];
            //     green_gy = (-1 * temp[i - 1][j - 1]) + (-2 * temp[i - 1][j]) + (-1 * temp[i - 1][j + 1]) + temp[i + 1][j - 1] + (2 * temp[i + 1][j]) + temp[i + 1][j + 1];
            //     blue_gy  = (-1 * temp[i - 1][j - 1]) + (-2 * temp[i - 1][j]) + (-1 * temp[i - 1][j + 1]) + temp[i + 1][j - 1] + (2 * temp[i + 1][j]) + temp[i + 1][j + 1];
            }

            image[i][j].rgbtRed   = round(sqrt(pow(round(red_gx), 2) + pow(round(red_gy), 2)));
            image[i][j].rgbtGreen = round(sqrt(pow(round(green_gx), 2) + pow(round(green_gy), 2)));
            image[i][j].rgbtBlue  = round(sqrt(pow(round(blue_gx), 2) + pow(round(blue_gy), 2)));
        }
    }
    return;
}

最佳答案

这里是 RGBTRIPLE 的定义:

RGBTRIPLE structure (wingdi.h)

typedef struct tagRGBTRIPLE {
  BYTE rgbtBlue;
  BYTE rgbtGreen;
  BYTE rgbtRed;
} RGBTRIPLE, *PRGBTRIPLE, *NPRGBTRIPLE, *LPRGBTRIPLE;

它是一个“结构”,其元素由 3 个字节组成。很明显,您不能只将其视为整数。 “2 * int(temp[i][j])”显然是非法的。

如果您愿意,您可以做的是将 rgbtBlue、rgbtGreen 和 rgbtRed 累加到一个 32 位整数中。也许是这样的:

unsigned int color = (temp[i][j].rgbtRed << 16) | (temp[i][j].rgbtGreen << 8) | (temp[i][j].rgbtBlue); 

你也可以定义一个 union .

但是为什么?也许您最好的选择是简单地将 R、G 和 B 视为“独立的”?

关于c - 将 type int 乘以 struct 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68989813/

相关文章:

c - 多维数组的值为 "skipped"的奇怪错误

c - 分割数组中的数字

c - FreeRTOS 中 mtCOVERAGE_TEST_MARKER 宏的用途

c - argc,argv 在 C 中的 Null 终止符中导致奇怪的行为

CS50 Pset1 现金错误 "expected identifier or ' ('"含义?

c - 从 C 中的递归二分搜索返回 bool 值

c - 在 Debian 中显示进程名称和 pid

c - "Wide character array initialized from non-wide string"是什么意思,我如何在 C 中更正它?

c++ - 用于 igraph_vector_t 和 igraph_matrix_t 的 GDB pretty-print

c - 有没有一种标准的方法来重建降低的结构函数参数?