通常将结构体的定义、类的定义放在独立的头文件中,只放在cpp文件中,导致访问的地方无法识别该结构体。通常情况下会将类型定义(头文件中)和变量定义(cpp文件)分离。
头文件中主要包含:类型定义、函数声明、全局变量声明、符号常量等内容。
cpp文件中主要包含:对象定义、函数定义、类成员函数定义等内容。
有一个小的示例:
dd.h中结构体定义
struct ttt
{
int age;
int weight;
};
ff.cpp中全局变量和容器对象定义
#include <vector>
#include "dd.h"
int abc=100;
std::vector<ttt> xyz;
ff.h中全局变量或变量的声明
#include <vector>
#include "dd.h"
extern int abc;
extern std::vector<ttt> xyz;
main.cpp中引用全局变量或对象
#include <iostream>
#include "ff.h"
using namespace std;
int main()
{
abc=50;
ttt temp;
xyz.push_back(temp);
return 0;
}