本文介绍了多个C ++文件中的共享向量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想分享(全球化)两个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;

现在,在链接步骤 V1 V2 将连接到 V1 V2 A.cpp (或者这些对象定义时) p>

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.

extern vector<uint64_t> V1;
extern vector<uint64_t> V2;

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 A.cpp.

In 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;

In 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;

Now, in the linking step V1 and V2 from B.cpp will be connected to the V1 and V2 from A.cpp (or whereever these objects are defined).

这篇关于多个C ++文件中的共享向量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:08