c++ 中的结构struct 可以在头文件中说明吗? 有的话,能不能给个例子

来源:百度知道 编辑:UC知道 时间:2024/06/08 18:14:28

可以,在源文件中包含头文件名后就可以用这个结构了

可以的。示例如下:
以下是 test.h文件的内容
#ifndef _TEST_H
#define _TEST_H

typedef struct test
{
int a;
flost b;
}Test;

#endif

以下是main.cpp文件的内容:

#include "test.h"
#include <iostream>
using namespace std;

void main(void)
{
Test demo;
demo.a = 10;
demo.b = 3.14;
cout<<demo.a<<" "<<demo.b<<endl;
}

简单的把struct定义的出来的东西看成是一个类型名 }后面跟的是变量(若有typedef除外)
你看int i;怎么写在.h里的就怎么写

可以下面是我写的希望对你有帮助!

第一个是头文件
struct ab
{
ab()
{
b='a';
}
int c;
char b;
};

这个是主函数

#include "ab.h"
#include <iostream>
using namespace std;
void main()
{
ab c;
cout<<c.b;
}