在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;
}

988

被折叠的 条评论
为什么被折叠?



