C++中b.cpp访问a.cpp中的vector<struct>变量 全局vector变量

在a.cpp中定义vector<struct>变量,想在b.cpp中访问。
网上查了做全局vector变量的教程,基本都是针对vector<int>这种,遇到中间是个struct的,直接套用报错。查询后得知,我是把struct定义在a.cpp中,b.cpp无法获取,这里只需要把struct定义到a.h中,b.cpp再引用即可。demo如下:

a.h

#include <atlstr.h>
struct student
{
	CString name;
	int age;
};

a.cpp

#include "a.h"
#include <atlstr.h>
#include <vector>

std::vector<student> students;
extern int main_b();

int main()
{
	printf("Hello this is a !\n");
	student s1; 

	s1.name = TEXT("koko");
	s1.age = 18;
	students.push_back(s1);
	printf("name:%S  age:%d\n",students[0].name,students[0].age);
	
	main_b();
	return 0;

}

b.cpp

#include <atlstr.h>
#include <vector>
#include "a.h"

extern std::vector<student> students;

int main_b()
{
	printf("Hello this is b !\n");
	printf("name:%S  age:%d \n", students[0].name, students[0].age);
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值