无效的操作数可转换为二进制表达式(' rgbtriple' and' int€#x27;)
当我尝试查看特定值是否存在时,我会遇到此错误。我使用了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类型
rgbtriple
是在 bmp.h 中定义的:它是一个结构:您不能将结构与
==
操作员进行比较,您必须比较会员单独。问题是:您是什么意思,请检查它是否为 是什么?
“像素是黑色的?
如果您的意思是 >:
映像[i -1] [i + 1]
有一个错字,i == 0
,<代码> j == 0 ,<代码> i ==高度 - 1 和j == width -1
)。这是一个简单的修复:
但是,请注意,上面的函数无法按编码工作,因为它覆盖将用于下一列和下一行的像素值。要执行此转换,您可以使用3行缓冲区来保持先前的值。
这是一个修改版本:
the type
RGBTRIPLE
is defined in bmp.h as: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
:You get a segmentation fault because you read pixels beyond the boundaries of the matrix
image
:image[i - 1][i + 1]
has a typoi == 0
,j == 0
,i == height - 1
andj == width - 1
).Here is a simple fix:
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:
您不能将标量类型与结构或数组进行比较。这就是错误消息告诉您的。
将多个无关字节的结构与数值进行比较应该是什么条件?
那不是它的工作方式。您只能分别比较字段或将整个结构与同一结构类型的另一个变量进行比较。
除此之外,您的代码中有一些误解。
您会得到一个分段故障,因为要读取的内存不是您的内存。您没有访问特权可以阅读或写作。
如果您为函数提供一些价值,它将始终“存在于内存中”。您已经在调用功能中提供了一个值。怎么不存在?
您无法通过查看复制值来检测复制的内存位置是否有效。
您的根本问题是,您没有在阅读值之前之前与数组的界限进行验证。
您必须将行和列索引与限制进行比较,并且仅在不超出界限时才访问数组元素。
您需要重新设计方法以收集像素以模糊。
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.
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.
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.