Menu

Difficulty reading a std::string from a LuaTe

Jon Capps
2011-12-09
2012-09-28
  • Jon Capps

    Jon Capps - 2011-12-09

    Hello, and thanks for directing me to the forum ;)

    I'm experimenting with lua_icxx to see how helpful it might be. I'm having
    trouble with this code:

    LuaTempResult ltr = _interpreter_ptr->eval( "return 'test'" );
    std::string ret = ltr;

    Based on the examples provided it seems it should work. However, I get the
    compiler error:

    c:\users\jon\documents\visual studio
    2010\projects\dron\dron\script\script.cpp(29): error C2440: 'initializing' :
    cannot convert from 'LuaTempResult::Item' to
    'std::basic_string<_Elem,_Traits,_Ax>'
    1> with
    1>
    1> No constructor could take the source type, or constructor overload
    resolution was ambiguous

    When I change the code somewhat, I get an error better describing the
    ambiguity:

    1>c:\users\jon\documents\visual studio
    2010\projects\dron\dron\script\script.cpp(27): error C2593: 'operator =' is
    ambiguous
    1> c:\program files (x86)\microsoft visual studio
    10.0\vc\include\xstring(772): could be 'std::basic_string<_Elem,_Traits,_Ax>
    &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
    1> with
    1>
    1> c:\program files (x86)\microsoft visual studio
    10.0\vc\include\xstring(767): or 'std::basic_string<_Elem,_Traits,_Ax>
    &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
    1> with
    1>
    1> c:\program files (x86)\microsoft visual studio
    10.0\vc\include\xstring(762): or 'std::basic_string<_Elem,_Traits,_Ax>
    &std::basic_string<_Elem,_Traits,_Ax>::operator =(const
    std::basic_string<_Elem,_Traits,_Ax> &)'
    1> with
    1>
    1> c:\program files (x86)\microsoft visual studio
    10.0\vc\include\xstring(707): or 'std::basic_string<_Elem,_Traits,_Ax>
    &std::basic_string<_Elem,_Traits,_Ax>::operator
    =(std::basic_string<_Elem,_Traits,_Ax> &&)'
    1> with
    1>
    1> while trying to match the argument list '(std::string,
    LuaTempResult::Item)'

    I'm running Win7 64bit, and using MSVC 2010 Express. Am I doing something
    wrong? How can I get this to work?

    Thanks.

     
  • schoenborno

    schoenborno - 2011-12-09

    Jon, glad you decided to post. On MS VS 2005 the following snippet works fine:

    void testDebug()
    {
        LuaInterpreter lua;
    
        LuaTempResult ltr = lua.eval( "'test'" );
        std::string ret = ltr[ 1 ];
    
        std::cout << ret << std::endl;
    }
    

    (This is not relevant to your problem but note that "eval" evaluates an
    expression, meaning you do not need to put "return": whatever the expression
    evaluates to is returned to the C++ side)

    It'll take me a couple days to try it in 2010 as I have been stuck with 2005
    for a long time.

    You could also try to bypass Item altogether and do

    LuaObjRef test = lua.eval("'test'"); 
    std::string msg = test;
    

    Also try even simpler

    std::string msg = lua.eval("'test'");
    

    Oliver

     
  • schoenborno

    schoenborno - 2011-12-09

    Jon, I also found that the last example I gave you gets a syntax error with
    "ambiguous operator=", which I solved by the following:

    std::string msg = (string)lua.eval("'test'");
    

    This makes me wonder if the following might be necessary in 2010:

    LuaTempResult ltr = lua.eval( "'test'" );
    std::string ret = string(ltr);
    

    (you don't need the because when not given, the first item returned is
    automatically used, though adding also works).
    Oliver

     
  • Jon Capps

    Jon Capps - 2011-12-09

    Thanks for the quick replies. I'll try your suggestions at my earliest
    opportunity.

    Jon

     
  • Jon Capps

    Jon Capps - 2011-12-09

    Yes, my errors were ambiguous operator= errors as well. Following your lead, I
    found that casting works:

        LuaTempResult ltr = _interpreter_ptr->eval( "'test'" );
        ret = static_cast< std::string >( ltr );
    

    I'm not knowledgeable enough to know why the 2010 compiler is more picky than
    the 2005 compiler, but if casting works, then I'm happy with it ;)

    Thanks for your help.

    Jon

     
  • schoenborno

    schoenborno - 2011-12-09

    Hi Jon, well it's good to hear that there is a workaround, but it certainly
    not great news if static_cast is the only way in VS 2010 Express. So you tried

    ret = string(ltr);
    

    and that did not work? Did you try

    std::string ret( ltr );
    

    I'm very surprised that these would fail if static_cast works.

    Oliver

     
  • Jon Capps

    Jon Capps - 2011-12-09

    Ah, my apologies. Yes, both those approaches work. I just prefer the C++-style
    casting since it's easier to spot when skimming code.

     
  • schoenborno

    schoenborno - 2011-12-09

    Ah that is good news, thanks for checking. Yes I agree that if you are
    going to cast, then static_cast<> is better than old-style cast. However I
    much prefer not to use casting at all, and instead use constructor or
    conversion (the two methods that I asked about in my last post). Cheers,
    Oliver

     

Log in to post a comment.

MongoDB Logo MongoDB