问题描述
我想分享(全球化)两个cpp文件(A.cpp和B.cpp)之间的一些向量变量(V1和V2)。
我已经通过以下命令在A.h中定义了V1和V2。
extern vector< uint64_t> V1;
extern vector< uint64_t> V2;
我也在A.cpp和B.CPP文件中添加了#includeA.h任何人都可以让我知道我应该做什么,以便能够访问这两个CPP文件中的V1和V2的元素?
感谢高级
首先,你需要选择你的向量应该定义的地方。假设您选择 A.cpp
。
在 A.cpp
(仅在一个文件中 - 在多个文件中定义相同的对象会产生多个定义的符号错误)将向量定义为全局变量:
矢量< uint64_t> V1;
矢量< uint64_t> V2;
在中B.cpp
您要访问的其他文件 V1
和 V2
)声明向量为 extern
。这将告诉链接器在别处搜索实际对象:
extern vector< uint64_t> V1;
extern vector< uint64_t> V2;
现在,在链接步骤 I want to share(globalize) some vector variables(V1 and V2) between two cpp files(A.cpp and B.cpp).I have already defined both V1 and V2 in A.h by the following commands. I also have added #include "A.h" to both A.cpp and B.CPP files. Can anyone let me know what else should I do to be able to access the elements of V1 and V2 in both of these CPP files? Thanks in Advance First, you need to pick up place where your vectors should be defined. Let's say that you choose In In Now, in the linking step 这篇关于多个C ++文件中的共享向量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! V1
V2
从将连接到
V1
和 V2
从 A.cpp
(或者这些对象定义时) p> extern vector<uint64_t> V1;
extern vector<uint64_t> V2;
A.cpp
.A.cpp
(only in one file - defining same object in multiple files will yield multiple defined symbols error) define vectors as global variables: vector<uint64_t> V1;
vector<uint64_t> V2;
B.cpp
(and in all other files from which you want to access V1
and V2
) declare vectors as extern
. This will tell linker to search elsewhere for the actual objects: extern vector<uint64_t> V1;
extern vector<uint64_t> V2;
V1
and V2
from B.cpp
will be connected to the V1
and V2
from A.cpp
(or whereever these objects are defined).