help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Exporting a variable to the global workspace from a C++ function


From: Justin Bare
Subject: Re: Exporting a variable to the global workspace from a C++ function
Date: Wed, 12 Aug 2009 10:40:01 -0400 (EDT)
User-agent: SquirrelMail/1.4.8-4.fc5

I tried this as you suggested:

    symbol_table::global_varref("x") = 9;
    symbol_table::set_scope(symbol_table::top_scope);
    symbol_table::insert("x").mark_global();

but I get compiler errors that say:

    readbuf.cpp:29: error: ‘global_varref’ is not a member of ‘symbol_table’
    readbuf.cpp:30: error: ‘set_scope’ is not a member of ‘symbol_table’
    readbuf.cpp:30: error: ‘top_scope’ is not a member of ‘symbol_table’
    readbuf.cpp:31: error: ‘insert’ is not a member of ‘symbol_table’

even though I have included the Octave/C++ library

How do I make this compile?


> On Wed, Aug 12, 2009 at 2:59 PM, Justin
> Bare<address@hidden> wrote:
>> I tried the function you gave me in this way inside a C++ function:
>>    string c = "x";
>>    const string& cref = c;
>>    octave_value x = 9;
>>    const octave_value& xref = x;
>>
>>    cout << cref << endl;
>>    cout << xref.int_value() << endl;
>>
>>    set_global_value(cref, xref);
>>    cout << get_global_value("x").int_value() << endl;
>>
>> Which prints out this in Octave:
>> x
>> 9
>> 9
>>
>> But what I want to do is create the variable x in this C++ function and
>> send it to the global workspace so that when I type "x" in the octave
>> command line it will give me "x = 9" instead of "error: `x' undefined
>> near
>> line 39 column 1"
>>
>> Is there a way to do this?
>> Thanks.
>>
>> Justin
>>
>
> Global variables are not by default visible from any workspaces; they
> need to be imported using the "global command".
>
> One possibility to do this completely from C++, using the 3.2.x API is
> (not actually tested):
>
> symbol_table::global_varref ("name") = value;
> symbol_table::set_scope (symbol_table::top_scope); // only needed if
> called from m-file function
> symbol_table::insert ("name").mark_global (); // will overwrite local
> variable if exists!
>
> another option is to call eval("global x;"), but that needs to invoke
> the parser and is thus slower.
>
> hth
>
> --
> RNDr. Jaroslav Hajek
> computing expert & GNU Octave developer
> Aeronautical Research and Test Institute (VZLU)
> Prague, Czech Republic
> url: www.highegg.matfyz.cz
>


-- 

Justin Bare
Robotic Research, LLC.
814 West Diamond Ave, Suite 301
Gaithersburg, MD 20878
240-631-0008 Ext 246


reply via email to

[Prev in Thread] Current Thread [Next in Thread]