头文件中声明变量

来源:百度知道 编辑:UC知道 时间:2024/06/09 07:29:36
/*********
* head.h
*********/
#ifndef _HEAD_H_
#define _HEAD_H_

//头文件
#include <stdio.h>

//学生结构体定义
struct Student
{
int id;
char name[10];
float mark[4];
struct Student *next;
};

//变量声明
extern int num;
extern struct Student *stus;

//函数声明
int ShowMainMenu(); //主菜单显示

#endif

/*****************
* main.cpp
****************/
#include "head.h"

static int num = 0;

struct Student *stus;
*stus = (struct Student *)malloc(sizeof(struct Student));

num = 2;
int main()
{
ShowMainMenu();
return 0;
}

这个一大堆的重定义错误问题, 到底变量在头文件中要怎么声明?
不对吧, #include <stdio.h>这个我已经放在头文件里面, 而在main.cpp里面我引用了头文件了。不关这个事的

变量和函数声明都是正确的
#include <stdio.h>放在main.cpp里面

#include <stdio.h>是一个已经定义好的库文件,放在main.cpp里面表示你在主函数里面用到这个库的函数,所以不需要放在头文件里面。