c++ - undeclared identifier using arrayfire -
i write function using arrayfire that:
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; }
when try call function as: abc()
in main program , try extract variables c
, d
, want print them using af_print(c)
, gives error:
error c2065: 'c' : undeclared identifier error c2065: 'd' : undeclared identifier intellisense: identifier "c" undefined intellisense: identifier "d" undefined
the main function is:
#include <cstdio> #include <math.h> #include <cstdlib> #include "test.h" // test.h contains function abc() , // arrayfire.h , // using namespace af; int main(int argc, char *argv[]) { abc(); // function // here calling 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; }
any solution please.
regards
in c there 3 scopes variables can defined in:
- global scope, when variables defined outside of functions.
- local scope, when variables declared in functions, scope include function arguments.
- block scope, variables defined in blocks nested in functions, example in body of
if
statement.
variables in 1 scope available in current scope, , nested scopes. don't exist in parallel scopes or scopes of higher level.
more "graphically" seen somethig this:
+---------------------+ | global scope | | | | +-----------------+ | | | function scope | | | | | | | | +-------------+ | | | | | block scope | | | | | +-------------+ | | | | | | | | +-------------+ | | | | | block scope | | | | | +-------------+ | | | +-----------------+ | | | | +-----------------+ | | | function scope | | | | | | | | +-------------+ | | | | | block scope | | | | | +-------------+ | | | +-----------------+ | +---------------------+
in figure above, there 2 function scopes. variables declared in 1 of function scopes can't used other function scope, local function.
same block scope, variables declared in block can used in block , children of block.
now how relates problem: variables c
, d
defined in function abc
, means scope in abc
function only, other functions (like main
function) can't see or access variables defined in abc
, variables local in scope of abc
function.
there many ways solve problem of accessing these variables other functions, , common beginner way put definition of variables in global scope. in function assign variables, like
array c; array d; void abc() { ... c = array(q, 1, a); d = array(q, 1, b); }
other solutions involve passing arguments reference , assigning them. or putting data in structure (class
or struct
) , returning instance of structure.
Comments
Post a Comment