使用arrayfire的未声明标识符
undeclared identifier using 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()
并尝试提取变量C
和D
并希望使用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
语句体中定义的变量。
一个作用域中的变量只在当前作用域和嵌套作用域中可用。它们只是不存在于并行作用域或更高级别的作用域中。
更"图形化"的,可以像这样看到:
<>之前+---------------------+|全局作用域|| || +-----------------+ |函数作用域| | | || | +-------------+ | |块范围| | || | +-------------+ | || | | || | +-------------+ | |块范围| | || | +-------------+ | || +-----------------+ || || +-----------------+ |函数作用域| | | || | +-------------+ | |块范围| | || | +-------------+ | || +-----------------+ |+---------------------+之前在上图中,有两个函数作用域。在一个函数作用域中声明的变量不能被任何其他函数作用域中使用,它们是该函数的局部。与块作用域相同,在块中声明的变量只能在该块和该块的子块中使用。
现在这与您的问题有关:变量C
和D
在函数ABC
中定义,这意味着它们的作用域仅在ABC
函数中,其他函数(如您的main
函数)无法看到或访问ABC
中定义的变量,变量在ABC
函数的作用域中是局部的。
array C;
array D;
void ABC()
{
...
C = array(Q, 1, A);
D = array(Q, 1, B);
}
其他解决方案包括通过引用传递实参并对其赋值。或者将数据放在结构 (class
或struct
)中并返回该结构的实例。
相关文章:
- #ifdef和未声明的标识符
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 使用未声明的标识符"sqrt"?
- 对类 (C++) 中的私有变量使用未声明的标识符
- 我的代码中有错误,未声明的标识符
- 未定义的标识符/未声明
- 错误:在 C++ 'std'使用未声明的标识符
- 在编译器之前检测未声明的标识符
- 使用未声明的标识符'temp'
- 使用未声明的标识符"yylex"和"yyin"?
- 使用"class"关键字,后跟未声明的标识符
- 未声明的标识符错误,但变量定义正确 (?)
- C2065 'cout':未声明的标识符
- C++模板模板参数中未声明的标识符
- 错误:在 C++ 目标 c 混合'self'使用未声明的标识符
- C++循环依赖关系,未声明的标识符
- 我的函数调用 (C++) 中的未声明标识符
- 未找到标识符和未声明的标识符