简体   繁体   English

编写C ++函数以对外部声明的数组进行操作

[英]Writing a C++ function to operate on arrays declared externally

I am trying to write a set of C++ functions ( ah , a.cpp ) that implement various operations on arrays. 我正在尝试编写一组实现数组上各种操作的C ++函数( aha.cpp )。 The actual arrays will be defined in other files ( bh , b.cpp , ch , c.cpp , etc.). 实际的数组将在其他文件( bhb.cppchc.cpp等)中定义。

My goal is that any project can #include "ah" and run these functions on arrays defined in that project. 我的目标是任何项目都可以#include "ah"并在该项目中定义的数组上运行这些功能。 I don't want to have to include anything in ah itself, because I want any future project to be able to use ah without rewriting it. 我不想在ah本身中包含任何内容,因为我希望将来的任何项目都可以使用ah而无需重写它。 But, I can't figure out how to use extern to do this. 但是,我不知道如何使用extern来做到这一点。

Here's a toy example of what I have so far. 这是到目前为止我所拥有的玩具示例。 a implements a function f , to be used on an as-yet unspecified array. a实现了一个函数f ,该函数将用于尚未指定的数组。

ah

// this is not right, but I'm not sure what to do instead
extern const int ARRAY_LEN;
extern int array[ARRAY_LEN]; // error occurs here

void f();

a.cpp a.cpp

#include "a.h"

// Do something with every element of "array"
void f() {
  for(int i=0; i < ARRAY_LEN; i++) {
    array[i];
  }
}

Now, project b defines the array and wants to use the function f on it. 现在,项目b定义了数组,并希望在其上使用函数f

bh BH

const int ARRAY_LEN = 3;

b.cpp cpp文件

#include "a.h"
#include "b.h"

int array[ARRAY_LEN] = {3, 4, 5};

// Some functions here will use f() from a.cpp

When I compile this, I get: 编译时,我得到:

In file included from b.cpp:1:0:
a.h:2:27: error: array bound is not an integer constant before ‘]’ token

I read these other questions which are related: 我阅读了以下其他相关问题:

... but I can't see how to apply the solutions to my case. ...但是我看不到如何将解决方案应用于我的案子。 The problem is that usually people end up #include -ing the files that define the array, and I want to do it the other way around: define the array in a new project, and #include the shared set of functions to operate on that array. 问题是通常人们最终会#include include-添加用于定义数组的文件,而我想以另一种方式来做:在新项目中定义数组,然后#include在该项目上操作的共享函数集数组。


Edit 1: If I replace the declaration of array in ah with the following, as suggested by @id256: 编辑1:如果我更换的声明arrayah用下面,通过@的建议id256:

extern int array[];

Then I get a different error: 然后我得到另一个错误:

multiple definition of `ARRAY_LEN'

Edit 2: I also tried the answer from: 编辑2:我也尝试从以下答案:

Why does "extern const int n;" 为什么“ extern const int n;” not work as expected? 不能按预期工作?

Basically, I added "extern const int ARRAY_LEN" to bh in order to "force external linkage". 基本上,我在bh中添加了“ extern const int ARRAY_LEN”,以“强制外部链接”。 So now: 所以现在:

bh BH

extern const int ARRAY_LEN;
const int ARRAY_LEN = 3;

.. and all other files are the same as originally. ..和所有其他文件与原始文件相同。 But I get the same original error: 但是我得到了相同的原始错误:

a.h:2:27: error: array bound is not an integer constant before ‘]’ token

When declaring an array as extern , you don't need to specify the size (for a multi-dimensional array, you still need all but the first dimension). 将数组声明为extern ,无需指定大小(对于多维数组,除第一维外,您还需要所有其他大小)。 Just use: 只需使用:

extern int array[];

Alternatively, include bh in ah (before declaring the array) so that the definition of ARRAY_LEN is visible when you declare the array. 或者,在ah中添加bh(在声明数组之前),以便在声明数组时可见ARRAY_LEN的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM