[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Dynamically loaded functions -- Using C++ associative arrays
From: |
E. Joshua Rigler |
Subject: |
Re: Dynamically loaded functions -- Using C++ associative arrays |
Date: |
Tue, 24 Jul 2001 17:24:09 -0600 |
This is NOT an answer to my original question, but rather a
demonstration of how an almost identical piece of code, except designed
as a standalone main() function, DOES work. Any ideas as to why trying
to do this in an Octave dynamically linked function would cause the
crash I mentioned previously? I am using RH-7.1's latest version of
G++.
Thanks in advance for any help.
-EJR
////////
//
// Test function for simple associative array
//
// test_map2.cc
//
////////
#include <iostream>
#include <string>
#include <map>
int main(int argc, char *argv[])
{
int i;
std::map<std::string,int> m1;
std::map<std::string,int>::iterator p;
m1["test1"] = 1;
m1["test2"] = 2;
m1["test10"] = 10;
for (i=1; i<argc; i++)
{
// Is there a matching key in our associative array
p = m1.find( argv[i] );
if (p != m1.end () )
{
// If there's a match, print its value
cout << m1[argv[i]] << "\n";
}
}
}
////////
//
// Test output from test_map2
//
////////
<~/src/octave/oct> ./test_map2 test1 test test10 test1 test1 test2 lala
1
10
1
1
2
<~/src/octave/oct>
"E. Joshua Rigler" wrote:
>
> First, please excuse my lack of C++ expertise, this is all fairly new to
> me.
>
> I am attempting to implement an associative array using the std::map<>
> class. It took me a while, but I think I finally figured it out, and
> even got a simple test function to compile with mkoctfile. However, I
> get an undefined symbol error when I try to use the function in octave,
> and octave crashes.
>
> I've attached my short test program, along with the error I get when I
> attempt to call it from octave. Do I need to link to a special library
> when I compile? What's going on here?
>
> Thanks,
>
> -EJR
>
> ////////
> //
> // Test function for simple associative array
> //
> // test_map.cc
> //
> ////////
>
> #include <octave/oct.h>
> #include <map>
>
> DEFUN_DLD (test_map, args, nargout,"...")
> {
>
> octave_value retval;
>
> static std::map<std::string,int> m1;
> static std::map<std::string,int>::iterator p;
>
> m1["test1"] = 1;
> m1["test2"] = 2;
> m1["test10"] = 10;
>
> // Is there a matching key in our associative array?
> p = m1.find(args(0).string_value () );
> if (p != m1.end() )
> {
> // If there's a match, return its value
> cout << m1[args(0).string_value ()];
> retval = 1.0;
> }
>
> return retval;
>
> }
>
> ////////
> //
> // Test output from test_map.oct
> //
> ////////
>
> 1092 <~/src/octave/oct> octave
> GNU Octave, version 2.1.33 (i686-pc-linux-gnu).
> Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.
> This is free software with ABSOLUTELY NO WARRANTY.
> For details, type `warranty'.
>
> octave:1> test_map ("test1")
> octave: error while loading shared libraries:
> /home/jrigler/src/octave/oct/test_map.oct: undefined symbol:
> lower_bound__t8_Rb_tree5Zt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0Zt4pair2ZCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0ZiZt10_Select1st1Zt4pair2ZCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0ZiZt4less1Zt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0Zt9allocator1ZiRCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------