struct 声明放在.h,定义在.c中,出现struct不可用的情况
kunp 2007-02-07 12:12:21 如果我把struct的定义和声明都放在.h中,在其他.c文件中使用这个struct是没有任何问题,但如果声明放在.h,定义在.c中,其他.c文件调用struct就会编译出错,各位有知道是为什么的么?
test.h
---------------------------------------------
#ifndef COMM_H
#define COMM_H
#include <stdio.h>
#include <stdlib.h>
struct test_st;
typedef struct test_st test_t;
void test();
#endif
----------------------------------------------
test.c
----------------------------------------------
#include "test.h"
struct test_st
{
int count;
char name[10];
};
----------------------------------------------
test2.c
----------------------------------------------
#include "test.h"
void test()
{
test_t t;
}
----------------------------------------------
编译时会报错:
----------------------------------------------
test2.c: In function `void test()':
test2.c:5: aggregate `test_t t' has incomplete type and cannot be defined
test2.c:5: warning: unused variable `<typeprefixerror> t'
-----------------------------------------------