首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

未定义的'stu1' 结构体的定义,有个小疑点,帮忙看看

2012-02-11 
未定义的stu1 结构体的定义,有个小问题,帮忙看看主程序-------------------------------structstudentst

未定义的'stu1' 结构体的定义,有个小问题,帮忙看看
主程序  
-------------------------------
struct   student   stu1;
int   main(int   argc,   char*   argv[])
{
        getStudentName();
        printf( "%d ",stu1.no);
        getch();
        return   0;
}
-------------------------------

a.h
-------------------------------
struct   student{
        int   no;
        char   *name;
};

void   getStudentName(void);
-------------------------------

a.c
-------------------------------
#include   "a.h "
//extern   struct   student   stu1;

void   getStudentName(void){
        stu1.id=5;
}
-------------------------------

报错:a.c里的         stu1.id=5;
[C++   Error]   a.c(5):   E2451   Undefined   symbol   'stu1 '
未定义的 'stu1 ',为什么?


[解决办法]
#include "a.h "
[解决办法]
重复包含了 "a.h "

用条件编译

#ifndef _A_H_H
#include "a.h "
#endif
[解决办法]
extern struct student stu1; 不能删
[解决办法]
a.h
-------------------------------
#ifndef _A_H_H
#define _A_H_H

struct student{
int no;
char *name;
};

void getStudentName(void);

#endif
-------------------------------

a.c
-------------------------------
#include "a.h "

struct student stu1;

void getStudentName(void){
stu1.id=5;
}
-------------------------------


主程序
-------------------------------

#include "a.h "

extern struct student stu1;

int main(int argc, char* argv[])
{
getStudentName();
printf( "%d ",stu1.no);
getch();
return 0;
}
-------------------------------

热点排行