c++ - 使用 arrayfire 的未声明标识符

标签 c++ function arrayfire

<分区>

我像这样使用 arrayfire 编写了一个函数:

int ABC()
{

static const int Q = 5;
double A[]  = { 0.0,  1.0,  0.0, 1.0, 1.0};
double B[]  = { 0.0, -1.0, -1.0, 1.0, 0.0 };
array C (Q, 1, A);
array D (Q, 1, B);

return 0;
}

当我尝试在主程序中将此函数调用为:ABC() 并尝试提取变量 CD 以及想使用 af_print(C) 打印它们,给出错误:

error C2065: 'C' : undeclared identifier
error C2065: 'D' : undeclared identifier
IntelliSense: identifier "C" is undefined
IntelliSense: identifier "D" is undefined

主要功能是:

#include <cstdio>
#include <math.h>
#include <cstdlib>
#include "test.h" 
// test.h contains the function ABC() and 
// arrayfire.h and 
// using namespace af; 

int main(int argc, char *argv[])

{
ABC(); // function
// here I am calling the variables defined in ABC()
af_print(C);
af_print(D);
#ifdef WIN32 // pause in Windows
if (!(argc == 2 && argv[1][0] == '-')) {
    printf("hit [enter]...");
    fflush(stdout);
    getchar();
}
#endif

return 0;
}

请提供任何解决方案。

问候

最佳答案

在 C 语言中,基本上可以定义三个作用域变量:

  • 全局范围,当变量在任何函数之外定义时。
  • 局部作用域,当在函数中声明变量时,这个作用域包括函数参数。
  • block 范围,这适用于嵌套在函数中的 block 中定义的变量,例如在 if 语句的主体中。

一个范围内的变量仅在当前范围和嵌套范围内可用。它们根本不存在于并行作用域或更高级别的作用域中。

更“形象地”可以看到这样的东西:

+---------------------+
| Global scope        |
|                     |
| +-----------------+ |
| | Function scope  | |
| |                 | |
| | +-------------+ | |
| | | Block scope | | |
| | +-------------+ | |
| |                 | |
| | +-------------+ | |
| | | Block scope | | |
| | +-------------+ | |
| +-----------------+ |
|                     |
| +-----------------+ |
| | Function scope  | |
| |                 | |
| | +-------------+ | |
| | | Block scope | | |
| | +-------------+ | |
| +-----------------+ |
+---------------------+

In the figure above, there are two function scopes. Variables declared in one of the function scopes can't be used by any other function scope, they are local to that function.

Same with block scope, variables declared in a block can only be used in that block and children of that block.


Now for how this relates to your problem: The variables C and D are defined in the function ABC, that means their scope is in the ABC function only, other functions (like your main function) can't see or access the variables defined in the ABC, the variables are local in the scope of the ABC function.

There are many ways to solve the problem of accessing these variables from other functions, and the most common beginner way is to put the definition of those variables in the global scope. Then in the function you assign to the variables, something like

array C;
array D;

void ABC()
{
    ...
    C = array(Q, 1, A);
    D = array(Q, 1, B);
}

其他解决方案涉及通过引用传递参数并为它们赋值。或者将数据放入结构(classstruct)并返回该结构的实例。

关于c++ - 使用 arrayfire 的未声明标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197877/

相关文章:

c++ - 立方体上的纹理贴图出现错误

c++ - 如何遍历图形并沿途提取边缘权重?

javascript - 创建一个函数来排序表 React,JSX

cuda - ArrayFire 与原始 CUDA 编程?

arrayfire - 如何在ArrayFire中做行列式?

c++ - 在没有root权限的情况下安装NTL并在makefile中修改它的构建路径

c++ - 如何将变量用作原始 JSON 字符串中的数据?

javascript - 编写一个函数,返回 4 个数字中最大的一个

php - 使用连接在 HTML 标记内回显带有条件的 PHP 函数

c++ - ArrayFire:具有从主函数调用的 OpenCL 内核的函数