[C++-sig] Re: implicitly_convertible
David Abrahams
dave at boost-consulting.com
Mon Feb 2 23:34:47 CET 2004
"aashish" <aashish at vrac.iastate.edu> writes:
> Hi,
>
> I wanted to have a function like this where when I pass a list I will either
> get return as set of integers or std::vector of double.
>
> I tried to implement the function in this manner .....
>
> C++--------------------------------
> int make_vector(bp::list vlist)
> {
> //std::vector<double> vector_list;
> int k = extract<int>(vlist.attr("__len__")());
>
> /*for (int i=0;i<10;i++)
> {
>
> vector_list.push_back(extract<double>(lt.attr("__getitem__")(i)));
That's the hard/ugly way.
Why not:
vector_list.push_back(extract<double>(lt[i]));
??
> }
>
> return vector_list;*/
>
> int kk = extract<int>(vlist.attr("__getitem__")(0));
>
> return kk;
> }
> Python-----------------------------
>
> L = ['1','2','3']
> a = make_vector(L)
> print a
>
> The problem is that I am getting the correct value for 'k' here but then it
> does throws error saying that
>
> Tracekack(most recent call last)
> File "pytest2.py" , line 30, in ?
> A = "make.vector"(L)
^^^^^^^^^^^^^
I can't believe that you copied this error message correctly.
> Typeerror: No registered converter was able to produce a C++ rvalue of type
> int from this Python object of type str.
>
> Now what does this error mean??
The error means that you tried to extract<int> from a Python string
object.
> And How can sole this problem.
Store ints instead of strings in L
L = [ 1, 2, 3 ]
??
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list