使用arrayfire的未声明标识符

undeclared identifier using arrayfire

本文关键字:标识符 未声明 arrayfire 使用      更新时间:2023-10-16

我用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语言中,基本上可以定义三种作用域变量:

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

一个作用域中的变量只在当前作用域和嵌套作用域中可用。它们只是不存在于并行作用域或更高级别的作用域中。

更"图形化"的,可以像这样看到:

<>之前+---------------------+|全局作用域|| || +-----------------+ |函数作用域| | | || | +-------------+ | |块范围| | || | +-------------+ | || | | || | +-------------+ | |块范围| | || | +-------------+ | || +-----------------+ || || +-----------------+ |函数作用域| | | || | +-------------+ | |块范围| | || | +-------------+ | || +-----------------+ |+---------------------+之前在上图中,有两个函数作用域。在一个函数作用域中声明的变量不能被任何其他函数作用域中使用,它们是该函数的局部

与块作用域相同,在块中声明的变量只能在该块和该块的子块中使用。


现在这与您的问题有关:变量CD在函数ABC中定义,这意味着它们的作用域仅在ABC函数中,其他函数(如您的main函数)无法看到或访问ABC中定义的变量,变量在ABC函数的作用域中是局部的。

有许多方法可以解决从其他函数访问这些变量的问题,最常见的初学者方法是将这些变量的定义放在全局作用域中。然后在函数中给变量赋值,比如
array C;
array D;
void ABC()
{
    ...
    C = array(Q, 1, A);
    D = array(Q, 1, B);
}

其他解决方案包括通过引用传递实参并对其赋值。或者将数据放在结构 (classstruct)中并返回该结构的实例。