c – 错误:在非结构或联合的情况下请求成员xxxxxx
这是一个旨在使用ppm图像文件的程序。
我在尝试使用接受全局结构变量并提取该图像成员的函数时遇到编译错误。
这是全局结构(在ppmIO.c和ppmIO.h中声明):
ppmIO.c:
struct Image *instance;
ppmIO.h:
struct Image { int width; int height; unsigned char *data; }; extern struct Image *instance;
这就是我从main调用我的函数的方法:
ImageInvert(&instance);
这些是我的imageManip.c文件的相关部分:
#include #include #include #include #include void ImageInvert(struct Image **toInvert) { int i; int pix = (*toInvert->width) * (*toInvert->height); for (i = 0; i data = ((unsigned char)255 - *(toInvert)->data)); *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++)); *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++)); } }
这是我的imageManip.h文件:
#include void ImageInvert(struct Image **toInvert); void ImageSwap(struct Image **toSwap);
这些是我得到的错误:
imageManip.c:31:23: error: request for member 'width' in something not a structure or union int pix = (*toInvert->width) * (*toInvert->height); ^ imageManip.c:31:44: error: request for member 'height' in something not a structure or union int pix = (*toInvert->width) * (*toInvert->height); ^ imageManip.c:35:18: error: request for member 'data' in something not a structure or union *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data)); ^ imageManip.c:35:60: error: request for member 'data' in something not a structure or union *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data)); ^ imageManip.c:35:67: error: expected ';' before ')' token *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data)); ^ imageManip.c:35:67: error: expected statement before ')' token imageManip.c:36:18: error: request for member 'data' in something not a structure or union *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++)); ^ imageManip.c:36:60: error: request for member 'data' in something not a structure or union *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++)); ^ imageManip.c:36:69: error: expected ';' before ')' token *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++)); ^ imageManip.c:36:69: error: expected statement before ')' token imageManip.c:37:18: error: request for member 'data' in something not a structure or union *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++)); ^ imageManip.c:37:60: error: request for member 'data' in something not a structure or union *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++)); ^ imageManip.c:37:69: error: expected ';' before ')' token *(toInvert)->data = ((unsigned char)255 - *(toInvert)->data++));
不确定我是否正确访问了成员,或者我是否正确使用指针…
我是如何使用括号的。
这是imageInvert函数的工作版本:
void ImageInvert(struct Image **toInvert) { int i; int pix = (*toInvert)->width * (*toInvert)->height; for (i = 0; i < pix; i++) { (*toInvert)->data = ((unsigned char)255 - (unsigned char)(*toInvert)->data); (*toInvert)->data = ((unsigned char)255 - (unsigned char)(*toInvert)->data++); (*toInvert)->data = ((unsigned char)255 - (unsigned char)(*toInvert)->data++); } }