C:取消引用指向不完整类型错误的指针
我试图在OS X上编译的项目是: 很遗憾,我无法修复以下行的问题:C:取消引用指向不完整类型错误的指针,c,reference,struct,C,Reference,Struct,我试图在OS X上编译的项目是: 很遗憾,我无法修复以下行的问题: width = info_ptr->width; height = info_ptr->height; ncol = 2 << (info_ptr->bit_depth - 1); read_png函数的完整代码如下: #include <png.h> #include <math.h> png_byte bit_depth; png_structp png_ptr;
width = info_ptr->width;
height = info_ptr->height;
ncol = 2 << (info_ptr->bit_depth - 1);
read_png函数的完整代码如下:
#include <png.h>
#include <math.h>
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;
int
read_png (char *fname)
{
char header [8];
FILE *in;
int i, j, ncol, rc;
if (! strcmp (fname, "-")) {
/* read from stdin: */
vprintf ("info: not trying to read png from stdin\n");
return -1;
}
if (! (in = fopen (fname, "rb"))) {
fprintf (stderr, "cannot open `%s'; reason: %s\n", fname,
strerror (errno));
return -1;
}
if (! in || (rc = fread (header, 1, 8, in)) != 8
|| png_sig_cmp ((unsigned char *) header, 0, 8) != 0) {
return -1;
}
if (! (png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0))
|| ! (info_ptr = png_create_info_struct (png_ptr))) {
return -1;
}
png_init_io (png_ptr, in);
png_set_sig_bytes (png_ptr, 8);
png_read_png (png_ptr, info_ptr,
PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA
| PNG_TRANSFORM_EXPAND, NULL);
/** | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_SHIFT **/
row_pointers = png_get_rows (png_ptr, info_ptr);
width = info_ptr->width;
height = info_ptr->height;
ncol = 2 << (info_ptr->bit_depth - 1);
vprintf ("info: got %d x %d pixel with %d cols\n", width, height, ncol);
alloc_cells (width, height);
for (j = 0; j < height; j++) {
png_byte *row = row_pointers [j];
for (i = 0; i < width; i++) {
png_byte *ptr = & row [i * 3];
/* ncol always 256 ? */
int r = (ptr [0] * 256) / ncol;
int g = (ptr [1] * 256) / ncol;
int b = (ptr [2] * 256) / ncol;
int col = ((r * 256 + g) * 256) + b;
int col_idx = get_color_idx (col);
if (col_idx < 0) {
if (unknown_color == -1) {
fprintf (stderr, "cannot read from `%s'; reason: invalid color found\n",
fname);
return -1;
} else {
/* set to black or white: */
col_idx = (unknown_color == 0 ? c_black : c_white);
}
}
set_cell (i, j, col_idx);
}
}
return 0;
}
#包括
#包括
png字节位深度;
png_structp png_ptr;
png_infop info_ptr;
int通过次数;
png_字节*行指针;
int
read_png(char*fname)
{
字符头[8];
文件*in;
int i,j,ncol,rc;
如果(!strcmp(fname,“-”)){
/*从标准文本中读取:*/
vprintf(“信息:不尝试从标准格式读取png\n”);
返回-1;
}
如果(!(in=fopen(fname,“rb”){
fprintf(stderr,“无法打开“%s”;原因:%s\n”,fname,
斯特雷罗(埃尔诺));
返回-1;
}
如果(!in | |)(rc=fread(头,1,8,in))!=8
||png_sig_cmp((无符号字符*)头,0,8)!=0){
返回-1;
}
if(!(png_ptr=png_create_read_struct(png_LIBPNG_VER_STRING,0,0))
||!(info_ptr=png_create_info_struct(png_ptr))){
返回-1;
}
png_init_io(png_ptr,in);
png_set_sig_字节(png_ptr,8);
png_read_png(png_ptr,info_ptr,
PNG_变换_条带_16 | PNG_变换_条带_ALPHA
|PNG_变换_展开,空);
/**| PNG_变换|打包| PNG_变换|移位**/
行指针=png\u获取行(png\u ptr,info\u ptr);
宽度=信息->宽度;
高度=信息->高度;
ncol=2位(深度-1);
vprintf(“信息:获得了%d x%d个像素,带有%d列\n”、宽度、高度、ncol);
同种异体细胞(宽度、高度);
对于(j=0;j
您需要查看png.h
(或其文档),找出png\u infop
的指针类型,然后找出应该如何访问其字段。假设这个指针确实是从中获取数据的正确对象,那么您需要从其他头中包含该类型的定义(以便编译器知道其数据成员width
等),或者您应该调用一些getter函数,这些函数接受png\u infop
参数并返回所需的信息
[编辑:看起来好像您应该使用
png\u-get\u-IHDR
,或png\u-get\u-image\u-width
等]您需要查看png.h
(或其文档),找出png\u-infop
的指针类型,然后了解如何访问其字段。假设这个指针确实是从中获取数据的正确对象,那么您需要从其他头中包含该类型的定义(以便编译器知道其数据成员width
等),或者您应该调用一些getter函数,这些函数接受png\u infop
参数并返回所需的信息
[编辑:看起来好像您应该使用
png\u get\u IHDR
,或png\u get\u image\u width
等]如果在头文件中有转发声明,并且未在源文件中包含相应类的文件,则通常会出现此错误:
//header.h
class B;
class A{
A();
B* b;
}
//source.cpp
#include "header.h"
//include "B.h" //include header where B is defined to prevent error
A::A()
{
b->foo(); //error, B is only a forward declaration
}
因此,您需要包含适当的标头,转发声明是不够的。如果标头中有转发声明,而源文件中没有包含相应类的文件,则通常会出现此错误:
//header.h
class B;
class A{
A();
B* b;
}
//source.cpp
#include "header.h"
//include "B.h" //include header where B is defined to prevent error
A::A()
{
b->foo(); //error, B is only a forward declaration
}
因此,您需要包含适当的标题,转发声明是不够的。我认为这是由
png.h
模块的创建者设计的
应该是png\u infop
在“png.h”
中声明为指向结构的指针。实际的结构声明和定义应该在“png.c”
中
作者不想公开struct
的内部结构,因此struct
是在“png.c”
中定义的
这意味着您无法访问结构的任何成员(即:info\u ptr->width
,info\u ptr->height
,info\u ptr->bit\u depth
)
struct
成员不允许用户访问
如果作者认为您需要
width
、height
或bit\u depth
信息(即:getWidth(info\u ptr)
、getHeight(info\u ptr)
,…),我想这是由png.h
模块的创建者设计的
应该是png\u infop
在“png.h”
中声明为指向结构的指针。实际的结构声明和定义应该在“png.c”
中
作者不想公开struct
的内部结构,因此struct
是在“png.c”
中定义的
这意味着您无法访问结构的任何成员(即:info\u ptr->width
,info\u ptr->height
,info\u ptr->bit\u depth
)
struct
成员不允许用户访问
如果作者认为您需要width
,height
或bit\u depth
信息(即:getWidth(info\u ptr)
,getHeight(info\u ptr)
,…),我打赌会有一些函数来访问这些成员
git clone https://github.com/Ramblurr/PietCreator.git
cd PietCreator
mkdir build
cd build
cmake ../
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for Q_WS_X11
-- Looking for Q_WS_X11 - not found.
-- Looking for Q_WS_WIN
-- Looking for Q_WS_WIN - not found.
-- Looking for Q_WS_QWS
-- Looking for Q_WS_QWS - not found.
-- Looking for Q_WS_MAC
-- Looking for Q_WS_MAC - found
-- Looking for QT_MAC_USE_COCOA
-- Looking for QT_MAC_USE_COCOA - found
-- Found Qt-Version 4.7.4 (using /usr/local/bin/qmake)
-- Looking for gdImagePng in /usr/local/lib/libgd.dylib
-- Looking for gdImagePng in /usr/local/lib/libgd.dylib - found
-- Found ZLIB: /usr/include (found version "1.2.3")
-- Found PNG: /usr/X11R6/lib/libpng.dylib
-- Looking for gdImageJpeg in /usr/local/lib/libgd.dylib
-- Looking for gdImageJpeg in /usr/local/lib/libgd.dylib - found
-- Found JPEG: /usr/local/lib/libjpeg.dylib
-- Looking for gdImageGif in /usr/local/lib/libgd.dylib
-- Looking for gdImageGif in /usr/local/lib/libgd.dylib - found
-- Found GD: /usr/local/lib/libgd.dylib
-- Found GIF: /usr/local/lib/libgif.dylib
-- Looking for include files HAVE_GD_H
-- Looking for include files HAVE_GD_H - found
-- Looking for include files HAVE_PNG_H
-- Looking for include files HAVE_PNG_H - not found.
-- Looking for include files HAVE_GIF_LIB_H
-- Looking for include files HAVE_GIF_LIB_H - found
-- Configuring done
-- Generating done
-- Build files have been written to: /Developer/workspace/png/PietCreator/build
Linking CXX executable pietcreator
[ 95%] Built target pietcreator
[ 97%] Generating NPietTest.moc
Scanning dependencies of target npiettest
[100%] Building CXX object npiet/CMakeFiles/npiettest.dir/test/NPietTest.cpp.o
Linking CXX executable npiettest
[100%] Built target npiettest
struct foo;
int bar(void) {
struct foo *p;
p->a = 0;
}
/*
* foo.h part of foo API
*/
struct foo;
extern void foo_set_a(struct foo *p, int value);
extern int foo_get_a(struct foo *p);
/*
* foo.c ... implements foo API
*/
struct foo {
int a;
};
void foo_set_a(struct foo *p, int value) {
p->a = value;
}
int foo_get_a(struct foo *p) {
return p->a;
}
use_foo() {
struct foo *my_foo;
foo_set_a(my_foo, 1);
}